C# .Net - How do I split a string that varies in the words and characters within it?
I need to split up some strings, but the number of characters and position will change, This is quite easy in PHP but seems more complicated in C#.
These are greyhound results in the UK.
I have these strings in an array, I need to extract almost everything from each string, So I need a quick and simple solution to this. I need to be able to extract the Date, T开发者_如何学JAVAime, Course(Crayfd), Distance(540m), also the Winner only(no "Winner(s): ", need to remove this), and also the marketID from the URL.
So what built in c# functions would be best suited for all this?, a small example of some c# functions and how I use them would be great. Also a small explanation would also be great.
[0, 0] = "BAGS cards / Crayfd 2nd Jul - 12:58 S6 540m settled"
[0, 1] = "Winner(s): Springtown Mary"
[0, 2] = "http://rss.betfair.com/Index.aspx?format=html&sportID=4339&marketID=103165302"
[1, 0] = "BAGS cards / Crayfd 2nd Jul - 12:58 TO BE PLACED settled"
[1, 1] = "Winner(s): Black Hawk Boy, Springtown Mary"
[1, 2] = "http://rss.betfair.com/Index.aspx?format=html&sportID=4339&marketID=103165303"
[2, 0] = "Forecast Betting / Crayfd (FC) 2nd July - 12:58 Forecast settled"
[2, 1] = "Winner(s): 1 - 3"
[2, 2] = "http://rss.betfair.com/Index.aspx?format=html&sportID=4339&marketID=103164570"
[3, 0] = "BAGS cards / Romfd 2nd Jul - 12:49 A2 400m settled"
[3, 1] = "Winner(s): Come On Rosie"
[3, 2] = "http://rss.betfair.com/Index.aspx?format=html&sportID=4339&marketID=103165272"
I'd suggest using String.Split, String.[Last]IndexOf, String.Substring, and LINQ extensions (simple ones, e.g. .Last()
, just to simplify things).
e.g. if the URL is in string url
and it's safe to assume the marketId is always at the end like that:
int marketId = int.Parse(url.Split('=').Last());
Or to get things from the first line, if it's called courseEtc
:
string[] courseEtcParts = courseEtc.Split('/', '-');
string[] lastParts = courseEtcParts.Split();
string time = lastParts[0];
And to look for something like a distance, you could use regex. Something like [0-9]+m
.
Generally, I would go about such a problem as follows:
First, determine if you actually need to split a string, or if you need to extract from it. Splitting seems appropriate when you want to get several alike things, ie. things with content of the same sort (e.g. lists). Extracting seems more appropriate where the single segments contain different things. In your case, the latter seems more likely.
If you were to split strings, you could use two methods:
string.Split
if each segment is separated by the same character(s).Regex.Split
if the segments' separators are not always the same.
For extracting, you'll almost invariably end up with regular expressions, ie. the System.Text.RegularExpressions.Regex
class. You need to find one or more patterns that matches your exact requirements.
To actually extract text with a regex, you can use capturing and non-capturing groups. Use normal ( )
parentheses to capture text matching a certain pattern; use non-capturing parentheses (?: )
for everything else. For example,
A (B) (?:C) D
B
will get captured in a group and be accessible e.g. via regex.Groups[1].Value
while C
will not get captured.
It's not entirely clear what your requirements are, but if you are splitting up strings, my first thought would be to use Regex:
http://msdn.microsoft.com/en-us/library/30wbz966(v=vs.71).aspx
Would that work for you? Hope this helps, John
精彩评论