C # string.split () separate string in uppercase

I used the Split() method to split the lines. But this work, if you set some character for the condition in string.Split() . Is there a way to split the string when Uppercase displayed?

Is it possible to get a few words from some not divided line, for example:

 DeleteSensorFromTemplate 

And the result line should look like this:

 Delete Sensor From Template 
+11
string split c #
source share
3 answers

Use Regex.split

 string[] split = Regex.Split(str, @"(?<!^)(?=[AZ])"); 
+21
source share

If you don't like RegEx and you just want to insert the missing spaces, this will also do the job:

 public static string InsertSpaceBeforeUpperCase(this string str) { var sb = new StringBuilder(); char previousChar = char.MinValue; // Unicode '\0' foreach (char c in str) { if (char.IsUpper(c)) { // If not the first character and previous character is not a space, insert a space before uppercase if (sb.Length != 0 && previousChar != ' ') { sb.Append(' '); } } sb.Append(c); previousChar = c; } return sb.ToString(); } 
+4
source share

Another regular expression method:

 public static string SplitCamelCase(string input) { return System.Text.RegularExpressions.Regex.Replace(input, "([AZ])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim(); } 
0
source share

All Articles