How do I make letters to uppercase after each of a

2019-07-16 01:24发布

问题:

I have a collection of characters (',', '.', '/', '-', ' ') then I have a collection of strings (about 500).

What I want to do as fast as possible is: after each of the characters I want to make the next letter uppercase.

I want the first capitalized as well and many of the strings are all uppercase to begin with.

EDIT: I modified tdragons answer to this final result:

    public static String CapitalizeAndStuff(string startingString)
    {
        startingString = startingString.ToLower();
        char[] chars = new[] { '-', ',', '/', ' ', '.'};
        StringBuilder result = new StringBuilder(startingString.Length);
        bool makeUpper = true;
        foreach (var c in startingString)
        {
            if (makeUpper)
            {
                result.Append(Char.ToUpper(c));
                makeUpper = false;
            }
            else
            {
                result.Append(c);
            }
            if (chars.Contains(c))
            {
                makeUpper = true;
            }
        }
        return result.ToString();
    }

Then I call this method for all my strings.

回答1:

string a = "fef-aw-fase-fes-fes,fes-,fse--sgr";
char[] chars = new[] { '-', ',' };
StringBuilder result = new StringBuilder(a.Length);
bool makeUpper = true;
foreach (var c in a)
{
    if (makeUpper)
    {
        result.Append(Char.ToUpper(c));
        makeUpper = false;
    }
    else
    {
        result.Append(c);
    }
    if (chars.Contains(c))
    {
        makeUpper = true;
    }
}


回答2:

public static string Capitalise(string text, string targets, CultureInfo culture)
{
    bool capitalise = true;
    var result = new StringBuilder(text.Length);

    foreach (char c in text)
    {
        if (capitalise)
        {
            result.Append(char.ToUpper(c, culture));
            capitalise = false;
        }
        else
        {
            if (targets.Contains(c))
                capitalise = true;

            result.Append(c);
        }
    }

    return result.ToString();
}

Use it like this:

string targets = ",./- ";
string text = "one,two.three/four-five six";

Console.WriteLine(Capitalise(text, targets, CultureInfo.InvariantCulture));


回答3:

        char[] chars = new char[] { ',', '.', '/', '-', ' ' };
        string input = "Foo bar bar foo, foo, bar,foo-bar.bar_foo zz-";
        string result = input[0] + new string(input.Skip(1).Select((c, i) => 
                  chars.Contains(input[i]) ? char.ToUpper(input[i + 1]) : input[i + 1]
             ).ToArray());
        Console.WriteLine(result);


回答4:

Or you can use a simply regex expression:

var result = Regex.Replace(str, @"([.,-][a-z]|\b[a-z])", m => m.Value.ToUpper());


回答5:

You can stringSplit your whole string multiple times, once for each element, rinse and repeate, and then uppcase each block.

char[] tempChar = {',','-'};
List<string> tempList = new List();
tempList.Add(yourstring);
foreach(var currentChar in tempChar)
{
  List<string> tempSecondList = new List();
  foreach(var tempString in tempList)
  {
    foreach(var tempSecondString in tempString.split(currentchar))
    {
      tempSecondList.Add(tempSecondString);
    }
  }
  tempList = tempSecondList;
}

I hope i did count correct, anyway, afterwards make every entry in tempList Upper