I have a string like this:
"o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467"
How do I extract only "o1 1232.5467"
?
The number of characters to be extracted are not the same always. Hence, I want to only extract until the second space is encountered.
This question is related to
c#
.net
string
string-parsing
There are shorter ways of doing it like others have said, but you can also check each character until you encounter a second space yourself, then return the corresponding substring.
static string Extract(string str)
{
bool end = false;
int length = 0 ;
foreach (char c in str)
{
if (c == ' ' && end == false)
{
end = true;
}
else if (c == ' ' && end == true)
{
break;
}
length++;
}
return str.Substring(0, length);
}
I was thinking about this problem for my own code and even though I probably will end up using something simpler/faster, here's another Linq solution that's similar to one that @Francisco added.
I just like it because it reads the most like what you actually want to do: "Take chars while the resulting substring has fewer than 2 spaces."
string input = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
var substring = input.TakeWhile((c0, index) =>
input.Substring(0, index + 1).Count(c => c == ' ') < 2);
string result = new String(substring.ToArray());
Just use String.IndexOf twice as in:
string str = "My Test String";
int index = str.IndexOf(' ');
index = str.IndexOf(' ', index + 1);
string result = str.Substring(0, index);
s.Substring(0, s.IndexOf(" ", s.IndexOf(" ") + 1))
:P
Just a note, I think that most of the algorithms here wont check if you have 2 or more spaces together, so it might get a space as the second word.
I don't know if it the best way, but I had a little fun linqing it :P (the good thing is that it let you choose the number of spaces/words you want to take)
var text = "a sdasdf ad a";
int numSpaces = 2;
var result = text.TakeWhile(c =>
{
if (c==' ')
numSpaces--;
if (numSpaces <= 0)
return false;
return true;
});
text = new string(result.ToArray());
I also got @ho's answer and made it into a cycle so you could again use it for as many words as you want :P
string str = "My Test String hello world";
int numberOfSpaces = 3;
int index = str.IndexOf(' ');
while (--numberOfSpaces>0)
{
index = str.IndexOf(' ', index + 1);
}
string result = str.Substring(0, index);
I would recommend a regular expression for this since it handles cases that you might not have considered.
var input = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
var regex = new Regex(@"^(.*? .*?) ");
var match = regex.Match(input);
if (match.Success)
{
Console.WriteLine(string.Format("'{0}'", match.Groups[1].Value));
}
string[] parts = myString.Split(" ");
string whatIWant = parts[0] + " "+ parts[1];
Something like this:
int i = str.IndexOf(' ');
i = str.IndexOf(' ', i + 1);
return str.Substring(i);
Use a regex: .
Match m = Regex.Match(text, @"(.+? .+?) ");
if (m.Success) {
do_something_with(m.Groups[1].Value);
}
You could try to find the indexOf "o1 " first. Then extract it. After this, Split the string using the chars "1232.5467":
string test = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
string header = test.Substring(test.IndexOf("o1 "), "o1 ".Length);
test = test.Substring("o1 ".Length, test.Length - "o1 ".Length);
string[] content = test.Split(' ');
string testString = "o1 1232.5467 1232.5467.........";
string secondItem = testString.Split(new char[]{' '}, 3)[1];
A straightforward approach would be the following:
string[] tokens = str.Split(' ');
string retVal = tokens[0] + " " + tokens[1];
Get the position of the first space:
int space1 = theString.IndexOf(' ');
The the position of the next space after that:
int space2 = theString.IndexOf(' ', space1 + 1);
Get the part of the string up to the second space:
string firstPart = theString.Substring(0, space2);
The above code put togehter into a one-liner:
string firstPart = theString.Substring(0, theString.IndexOf(' ', theString.IndexOf(' ') + 1));
Source: Stackoverflow.com