简体   繁体   中英

Spliting a string by capital letters except for certain keywords

The following function splits a string at any uppercase character found within the string.

public static string ToSentence(this string input)
{
   var list = new List<char>();
    for (var i = 0; i < input.ToCharArray().Length; i++)
    {
        var c = input.ToCharArray()[i];
        foreach (char c1 in i > 0 && char.IsUpper(c) ? new[] {' ', c} : new[] {c})
            list.Add(c1);
    }
    return new string(list.ToArray());
}

In my code, this function is being used in conjunction with another function that retrieves the name of the current method in code. I'm finding this function breaks when a method name contains multiple capital letters sequentially.

For example, if I have a method called GetDatabaseIDE() it would return as "Get Database IDE"

How can I change my ToSentence function so that it accepts a list of keywords that won't be split (For example, IDE becomes IDE )?

Why not try Regex ? Demo @ https://dotnetfiddle.net/FsPZ9O
1. ([AZ]+) - match all leading uppercase char.
2. ([^AZ])* - followed with zero-or-more of any that isn't an uppercase char.

Regex.Matches("GetDatabaseIDE", @"([A-Z]+)([^A-Z])*").Cast<Match>().Select(m => m.Value);

TakeWhile method can be useful here, once you find an upper case character, you can take the following upper case characters:

for (var i = 0; i < input.Length; i++)
{
    var c = input[i];
    if(char.IsUpper(c))
    {
        var charsToAdd = input.Skip(i).TakeWhile(char.IsUpper).ToArray();
        if(charsToAdd.Length > 1)
        {
            i += charsToAdd.Length - 1;
        }

        if(i > 0) list.Add(' ');

        list.Add(charsToAdd);
    }
    else 
    {
        list.Add(c);
    }
}

you can add keywords you want to skip:

public static string ToSentence(string input)
{
   var list = new List<char>();
    for (var i = 0; i < input.ToCharArray().Length; i++)
    {
            if(input.IndexOf("IDE",i,input.Length-i)==i){
                list.AddRange(" IDE");
                i+=2; 
            }
            else{
                var c = input.ToCharArray()[i];
                foreach (char c1 in i > 0 && char.IsUpper(c) ? new[] {' ', c} : new[] {c})
                list.Add(c1);
            }
     }
    return new string(list.ToArray());
}

watch it here

This will work for your example, you will have to tweak it if your method names have numbers in them

using System.Text.RegularExpressions;
public static string GetNiceName(string testString)
{
    var pattern = "([A-Z][a-z]+)|([A-Z]+)";
    var result = new List<string>();
    foreach (Match m in Regex.Matches(testString, pattern))
    {
        result.Add(m.Groups[0].Value);
    }
    return string.Join(" ", result);
}

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