Re: String Operation in C#

Tech-Archive recommends: Fix windows errors by optimizing your registry



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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Rhonda Tipton" <Rtipton12@xxxxxxxxxx> wrote in message
news:Lf84f.37887$Q53.6561@xxxxxxxxxxxxxxxxxxxxxxx
> 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
>


.



Relevant Pages

  • Re: String Operation in C#
    ... Rhonda ... > // Get the substring based on the indexes. ... >> I would like to be able to extract the part after the number and before ... >> TEST FACILITY 1 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Extract a substring of n digits from a string
    ... The position of the n digits are different from line to ... Finally extract the substring from the matching ... to simplify the matching and extraction of the substring pattern. ...
    (comp.unix.shell)
  • Re: Substring in VB
    ... "Veign" wrote in message ... >> How can I extract a substring from a string? ... >> How do I extract the substring by skipping over the leading "www."? ...
    (microsoft.public.vb.syntax)
  • Re: Substring in VB
    ... > How can I extract a substring from a string? ... > How do I extract the substring by skipping over the leading "www."? ...
    (microsoft.public.vb.syntax)
  • Substring in VB
    ... How can I extract a substring from a string? ... How do I extract the substring by skipping over the leading "www."? ...
    (microsoft.public.vb.syntax)