简体   繁体   中英

C# string.split() separate string by uppercase

I've been using the Split() method to split strings. But this work if you set some character for condition in string.Split() . Is there any way to split a string when is see Uppercase ?

Is it possible to get few words from some not separated string like:

DeleteSensorFromTemplate

And the result string is to be like:

Delete Sensor From Template

使用Regex.split

string[] split =  Regex.Split(str, @"(?<!^)(?=[A-Z])");

If you do not like RegEx and you really just want to insert the missing spaces, this will do the job too:

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();
}

Another way with regex:

public static string SplitCamelCase(string input)
{
   return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
}

I had some fun with this one and came up with a function that splits by case, as well as groups together caps (it assumes title case for whatever follows) and digits.

Examples:

Input -> "TodayIUpdated32UPCCodes"

Output -> "Today I Updated 32 UPC Codes"

Code (please excuse the funky symbols I use)...

public string[] SplitByCase(this string s) {
   var ʀ = new List<string>();
   var ᴛ = new StringBuilder();
   var previous = SplitByCaseModes.None;
   foreach(var ɪ in s) {
      SplitByCaseModes mode_ɪ;
      if(string.IsNullOrWhiteSpace(ɪ.ToString())) {
         mode_ɪ = SplitByCaseModes.WhiteSpace;
      } else if("0123456789".Contains(ɪ)) {
         mode_ɪ = SplitByCaseModes.Digit;
      } else if(ɪ == ɪ.ToString().ToUpper()[0]) {
         mode_ɪ = SplitByCaseModes.UpperCase;
      } else {
         mode_ɪ = SplitByCaseModes.LowerCase;
      }
      if((previous == SplitByCaseModes.None) || (previous == mode_ɪ)) {
         ᴛ.Append(ɪ);
      } else if((previous == SplitByCaseModes.UpperCase) && (mode_ɪ == SplitByCaseModes.LowerCase)) {
         if(ᴛ.Length > 1) {
            ʀ.Add(ᴛ.ToString().Substring(0, ᴛ.Length - 1));
            ᴛ.Remove(0, ᴛ.Length - 1);
         }
         ᴛ.Append(ɪ);
      } else {
         ʀ.Add(ᴛ.ToString());
         ᴛ.Clear();
         ᴛ.Append(ɪ);
      }
      previous = mode_ɪ;
   }
   if(ᴛ.Length != 0) ʀ.Add(ᴛ.ToString());
   return ʀ.ToArray();
}

private enum SplitByCaseModes { None, WhiteSpace, Digit, UpperCase, LowerCase }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM