How to do a Split for when there is a letter in the string?

10

I'd like to make a string be split with .Split() every time there is a letter. Example:

The string : 97A96D112A109X115T114H122D118Y128

A array would come with 9 values: { 97 96 112 109 115 114 122 118 128 }

How can I do this?

Code I have (to use as a basis):

string[] az = txt.Split(/*Parametros aqui*/);
    
asked by anonymous 01.06.2017 / 00:46

6 answers

14

Use Split() to break using a < in> array with all characters that may be broken. In order to make it easier, even if it is not the most performative (but not exaggerated), you can create a string and transform it into an array of char with ToCharArray() . Gives to do in a line. If you want maximum performance, create array in the hand.

If you need lowercase letters, just include them. Check the documentation for options if you need specific ways to break.

using static System.Console;
public class Program {
    public static void Main() {
        var partes = "97A96D112A109X115T114H122D118Y128".Split("ABCDEFGHIJKLMNOPQRSTUVXWYZ".ToCharArray());
        foreach (var item in partes) WriteLine(item); //só para confirmar que deu certo
    }
}

See running on .NET Fiddle . And No Coding Ground . Also I put it in GitHub for future reference .

    
01.06.2017 / 01:46
7

You could do this by using the char.IsLetter . would look like this:

string az= "97A96D112A109X115T114H122D118Y128";

string[] novaString;

foreach (char c in az)
{
    if(char.IsLetter(c)
    {
        string[] novaString = az.Split(c);
    }
}

Here you access like this: novaString[0] novaString[1]

    
01.06.2017 / 01:00
6

You can use a list with the characters needed to split your array:

using System.Collections.Generic;

namespace SplitTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var text = "97A96D112A109X115T114H122D118Y128";
            var separatorList = new List<char>();

            // ASCII TABLE
            // 0X41 = A
            // 0X5A = Z

            for (int i = 0X41; i < 0X5B; i++)
            {
                separatorList.Add((char)i);
            }

            var result = text.Split(separatorList.ToArray());

        }
    }
}
    
01.06.2017 / 01:05
6

Try this:

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string[] az = txt.Split(alphabet.ToCharArray());
    
01.06.2017 / 01:41
5

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('-');
    
01.06.2017 / 05:38
2

In addition to what has been answered previously you can use regular expression 'Regex' with the replace function, replacing the unwanted characters, in this case replace with a '-' dash, then just use 'Split' with that character - '.

 string txt = "97A96D112A109X115T114H122D118Y128";
 string[] az = Regex.Replace(txt, "[^0-9]", "-").Split('-');
    
02.06.2017 / 21:50