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, "");
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.
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.
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.
str = "Hello I'm your String"; String[] splited = str. split(" ");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With