usr021986 usr021986 - 11 months ago 58
C# Question

Better way to split a string "a_b_c_d."

what is the best ways to split sting "aaa_bx_cd_de.1000.20.3.40" to "aaa_bx_cd_de" and "1000.20.3.40" in application


Answer Source

You can use an overload of String.Split[1] that takes as a second argument the number of substrings you want returned (essentially the number of splits plus 1).

string mainstring = "a_b_c_d.";
string[] parts = string.Split(new [] {'.'}, 2);
// parts[0] will be "a_b_c_d"
// parts[1] will be ""