How to do a split of an integer inside string?

-1

Follow string:

11 blabalba, balbalba balballbal  baba
12 balbal13, afafaf14 1414adad1414

I want it to return something like this (separated by split):

array 0: 11

array 1: blabalba, balbalba balballbal  baba

Second line:

array 0: 12

array 1: balbal13, afafaf14 1414adad1414

Always in the first position.

    
asked by anonymous 08.11.2017 / 03:06

1 answer

1

You can use something like this:

public static Tuple<int, string, bool> CustomSplit(string text)
{
    int val = 0;
    string aux = "";
    string res = null;
    for(int i = 0; i < text.Length; i++)
    {
        if(text[i] >= 48 && text[i] <= 57)
        {
            aux += text[i];
            continue;
        }
        res = text.Substring(i);
        break;
    }
    bool ok = int.TryParse(aux, out val);
    return new Tuple<int, string, bool>(val, res, ok);
}

To use you just need to call it:

string teste1 = "11 blabalba, balbalba balballbal  baba";
string teste2 = "12 balbal13, afafaf14 1414adad1414";

Tuple<int, string, bool> res = CustomSplit(teste1);
Console.WriteLine("\nItem1: "+res.Item1+"\nItem2: "+res.Item2+"\nItem3: "+ res.Item3);

res = CustomSplit(teste2);
Console.WriteLine("\nItem1: "+res.Item1+"\nItem2: "+res.Item2+"\nItem3: "+ res.Item3);

It will return one more variable bool confirming if it found and was able to convert the number.

    
08.11.2017 / 03:40