Another alternative is to use regular expressions ( System.Text.RegularExpressions
) through a pattern in the Regex search 'language' to make matches of numbers, and then use Linq ( System.Linq
) to get the result, in the case of a collection, cast IEnumerable
, Match
typed get only the values we want in the 'value' property and then convert to an array of strings, of this array we use the IEnumerable
method of class Join
and then we use String
. In fact there is no need for Split
nor Join
and the code is simpler still. I leave both options for comparison.
1) Without Split
:
// varias linhas
string value = "97A96D112A109X1X15T114H122D118Y128";
var matches = Regex.Matches(value, "[0-9]+");
var arrayOfNumbers = matches.Cast<Match>().Select(m => m.Value).ToArray();
// uma linha apenas
string[] arrayOfNumbers = Regex.Matches("97A96D112A109X1X15T114H122D118Y128", "[0-9]+")
.Cast<Match>().Select(m => m.Value).ToArray();
2) With Split
:
string value = "97A96D112A109X1X15T114H122D118Y128";
var matches = Regex.Matches(value, "[0-9]+");
var arrayOfNumbers = String.Join("-", matches.Cast<Match>().Select(m => m.Value).ToArray()).Split('-');