tags:

views:

279

answers:

4

I have a string value which I need to get the middle bit out, e.g. "Cancel Payer" / "New Paperless".

These are an example of the string format:

"REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf"
"REF_SPHCPHJ0000056_New Paperless_20100105174151.pdf"

+4  A: 

Isn't this a place for regular expressions?

Regex re = new Regex(@".*_(?<middle>\w+ \w+)_.*?");
string name = "REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf";
string middle = re.Match(name).Groups["middle"].Value;
Rubens Farias
That seems like a bit of overkill to simply split a string.
George
I respect your opinion; ty for your comment
Rubens Farias
+14  A: 
string s = "REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf";
string middleBit = s.Split('_')[2];
Console.WriteLine(middleBit);

Output is

Cancel Payer
Jason
+1 Well, regex surely are powerfull, but when a good old split does the trick...
Cédric Rup
A: 

I think that this regular expression:

Regex re = new Regex(@"\w+_\w+_(?<searched>.*)_\d*.pdf");

will meet your needs, if the pdf files always come to you as

REF_<text>_<your text here>_<some date + id maybe>.pdf
Danail
A: 

Cant write comments yet... but have to say that Jasons solution will not work correctly in case there is an underscore in the "middle bit". It will return only the part before underscore.

x64igor