String Operation in C#

  • Thread starter Thread starter Rhonda Tipton
  • Start date Start date
R

Rhonda Tipton

Hello.

I am trying to do a substring or something like that to extract the middle
portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before the
_.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt
 
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(str.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of the
section you want to get, and an underscore immediately following it.

Hope this helps.
 
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda


Nicholas Paldino said:
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(str.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of the
section you want to get, and an underscore immediately following it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rhonda Tipton said:
Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt
 
Rhonda,

If you know there is always a five digit number, followed by a space,
you can reduce this code to:

// Assuming str has your string, you could do:
// The index of the space.
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(6, ulIndex - 6);


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rhonda Tipton said:
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda


Nicholas Paldino said:
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like
this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(str.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of
the section you want to get, and an underscore immediately following it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rhonda Tipton said:
Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt
 
Cool.... that works perfect for me. Thanks again for your help.


Nicholas Paldino said:
Rhonda,

If you know there is always a five digit number, followed by a space,
you can reduce this code to:

// Assuming str has your string, you could do:
// The index of the space.
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(6, ulIndex - 6);


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rhonda Tipton said:
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda


Nicholas Paldino said:
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like
this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_');

// Get the substring based on the indexes.
subStr = str.Substring(str.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of
the section you want to get, and an underscore immediately following it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt
 
Back
Top