Reputation: 51
Here's what I'm trying to workout split function.
The com
is a string passed from a textbox, and it removes the first piece from the text.
The text in the textbox is being pass as this ".fedex TYU-123 Time to pick up package"
Then, when gms
is part of the first piece of the strip[]
array.
string format = com.Remove(0,1);
string[] strip = format.Split(new string[] { " " }, StringSplitOptions.None);
if (strip[0] == "gsm")
{
string carrier = strip[0];
string trackid = strip[1];
string message = strip[2];
}
strip[2]
only contains "Time"
. I wanted to return the last part as this: "Time to pick up package"
.
Keep in mind also, since the message will be different at times as well, so I don't want a specific string search.
How can I achieve this?
Upvotes: 2
Views: 208
Reputation: 887225
It sounds like you only want to split the first three elements.
Split()
has an overload that lets you tell it how many items to return:
format.Split(new[] { ' ' }, 3)
Upvotes: 3
Reputation: 2835
You can use the string.Split(char[], int32) method for this, in which you can give a max. number of splitted strings to return.
So:
format.Split(new[] {' '}, 3);
would do the trick.
More info: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
Upvotes: 0
Reputation: 460028
I assume you want all words starting with the third, use string.Join(" ", strip.Skip(2))
:
string[] strip = format.Split(new string[] { " " }, StringSplitOptions.None);
if (strip[0] == "gsm")
{
string carrier = strip[0];
string trackid = strip[1];
string message = string.Join(" ", strip.Skip(2)); //Time to pick up package
}
Upvotes: 1