views:

376

answers:

4

If I have various strings that have text followed by whitespace followed by text, how can I parse the substring beginning with the first character in the second block of text?

For example: If I have the string:
"stringA stringB"

How can I extract the substring
"stringB"

The strings are of various lengths but will all be of the format .

I'm sure this can be easily done with regex but I'm having trouble finding the proper syntax for c#.

+2  A: 

No RegEx needed, just split it.

var test = "stringA stringB";
var second = test.Split()[1];

and if you are in the wonderful LINQ-land

var second = "string1 string2".Split().ElementAtOrDefault(1);

and with RegEx (for completeness)

var str2 = Regex.Match("str1 str2", @"\w (.*$)").Groups[1].Value;
Jonas Elfström
there does not exist such parameterless-method :)
Andreas Niedermair
Works fine for me. .NET 3.5
Jonas Elfström
due to compiler overload resolution!
Andreas Niedermair
You make it sound like that's a bad thing.
Jonas Elfström
+1  A: 

use string.Split()

var test = "stringA stringB";
var elements = test.Split(new[]
{
    ' '
});
var desiredItem = elements.ElementAtOrDefault(1);

if you want to capture all whitespaces (msdn tells us more):

var test = "stringA stringB";
//var elements = test.Split(); // pseudo overload
var elements = test.Split(null); // correct overload
var desiredItem = elements.ElementAtOrDefault(1);

edit:
why pseudo-overload?

  • .Split() gets compiled to .Split(new char[0])
  • not documented in MSDN
Andreas Niedermair
This will only parse spaces, not all whitespace (tabs, newlines).
Oded
solution is based explecitely on oos example! ... did an update anyhow!
Andreas Niedermair
What's a pseudo overload?
Jonas Elfström
see my update!..
Andreas Niedermair
A: 

If all strings are separated by a whitespace you don't need a regex here. You could just use the Split() method:

string[] result = { };
string myStrings = "stringA stringB stringC";
result = myStrings.Split(' ');
Simon Linder
A: 

You don't need event the Split(). I think a simple IndexOf/Substring will do the job.

        var input = "A B";
        var result = string.Empty;
        var index = input.IndexOf(' ');
        if (index >= 0)
        {
            result = input.Substring(index + 1);
        }
Petar Petrov
-, as you need to know the length of the desired item, and this is not the wished behaviour of oo
Andreas Niedermair
I don't get you comment. What's the problem with my solution ? Even the author says : "How can I extract the substring ?" SUBSTRING ! He wants just "stringB". Why using Split() ? Why creating an array and using only the last item ? Why using LINQ for this simple case ?
Petar Petrov
Your solution might be faster and using less advanced techniques but it's definitely not easier to read. At least not for me. No downvote from me though.
Jonas Elfström
Andreas misunderstanding on how it works might be a hint in some direction.
Jonas Elfström
nope ... i know how it works, but ... what if `var input = "A B C";`? result would be "B C" ... so for correct splitting, we need to know the length of the desired item ... (re-read question: saying "various strings", "for example: 'stringA stringB'", ...)
Andreas Niedermair