Reputation: 452
I'm trying to figure out how to extract unspecified characters from string.
For example: I have long string "1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1"
and so on. What I'm trying to do is to split the string after each number. I've followed previous tutorial to use string.split('1'), but I would need to extract the entire number part which can be different. Can I specifi something like string.split('*.*.*')
where star represents any number from 1-9?
Thanks
Upvotes: 0
Views: 129
Reputation: 1500435
I'd suggest using regular expressions. I believe you want:
That's represented by the regex of
\d+(\.\d+)*
\d
means "a digit"+
means "at least one"\.
means "a dot" (just .
would mean "any character")*
means "zero or more"Here's a complete example showing this:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
List<string> pieces = SplitToDottedNumbers("1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1");
foreach (string piece in pieces)
{
Console.WriteLine(piece);
}
}
static List<string> SplitToDottedNumbers(string text)
{
// Inline for readability. You could create this just once.
var regex = new Regex(@"\d+(\.\d+)*");
// LINQ-based implementation
return regex.Matches(text)
.Cast<Match>()
.Select(match => match.Value)
.ToList();
/* Alternative implementation
var pieces = new List<string>();
var match = regex.Match(text);
while (match.Success)
{
pieces.Add(match.Value);
match = match.NextMatch();
}
return pieces;
*/
}
}
(There may be a simpler way of extracting all values. You could call Matches
Upvotes: 3