Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the most efficient way to separate string

I have this string:

"B82V16814133260"

what would be the most efficient way to get two strings out of it:

left part String: "B82V" rigth part string: "16814133260"

The rule is this: take all numbers on the right side and create string out of them, then take the reminder and place it into another string.

This is my solution, but it is too bulky! How to do it short and efficient?

        String leftString = "";
        String rightString="";

        foreach (char A in textBox13.Text.Reverse())
        {
            if (Char.IsNumber(A))
            {
                rightString += A;
            }
            else
            {
                break;
            }
        }

        char[] arr = rightString.ToArray();
        Array.Reverse(arr);

        rightString=new string(arr);
        leftString = textBox13.Text.Replace(rightString, "");
like image 728
Andrew Avatar asked Mar 28 '12 18:03

Andrew


People also ask

How do you separate strings?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is the best way to split a string in Java?

You can use the split() method of java. lang. String class to split a string based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot.

What is the best way to split a string in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do you separate strings in space?

str = "Hello I'm your String"; String[] splited = str. split(" ");


1 Answers

This yields what you're expecting:

var given = "B82V16814133260";
var first = given.TrimEnd("0123456789".ToCharArray());
var rest = given.Substring(first.Length);

Console.Write("{0} -> {1} -- {2}", given, first, rest);
//  B82V16814133260 -> B82V -- 16814133260
like image 105
Austin Salonen Avatar answered Sep 29 '22 12:09

Austin Salonen