如何连接由 C# 中的 foreach 循环大写的文本?

How can I concatenate a text capitalized by a foreach loop in C#?

using System;

class Program{
  public static void Main (string[] args){
    string Text = "the sentence which each word must be capitalized";
    string[] WordArray = new string[8];

    foreach (string Word in Text.Split(' ')){
      string CapitalizedFirstLetter = Word.Substring(0, 1).ToUpper();
      string RestOfWord = Word.Substring(1, Word.Length-1);
      string ConcatenatedWord = string.Concat(CapitalizedFirstLetter, RestOfWord);
    }
  }
}

我打算将每个单词大写并再次连接,但我无法连接。 我应该如何连接它?

为了以防万一,你可以这样做:

string Text = "the sentence which each word must be capitalized";
Console.WriteLine(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text));

输出:

The Sentence Which Each Word Must Be Capitalized

一个有点不雅的方法是在进入列表时跟踪所有单词,然后在最后加入。

using System;
using System.Collections.Generic;

class Program{
  public static void Main (string[] args){
    string Text = "the sentence which each word must be capitalized";
    string[] WordArray = new string[8];    //Unused
    List<string> WordsCapitalized = new(); //New line

    foreach (string Word in Text.Split(' ')){
      string CapitalizedFirstLetter = Word.Substring(0, 1).ToUpper();
      string RestOfWord = Word.Substring(1, Word.Length-1);
      string ConcatenatedWord = string.Concat(CapitalizedFirstLetter, RestOfWord);
      WordsCapitalized.Add(ConcatenatedWord); //new line
    }

    string FinalString = String.Join(" ", WordsCapitalized); //" " is delimiter between words
  }
}

这是基于您发布的代码构建的,只是为了展示一种直接的实现方式。这不一定是我从头开始处理这个问题的方式。至少,如果是我的代码,我会清理它并减少这种方法中的变量数量。

你可以试试这个

string text = "the sentence which each word must be capitalized";

var words = text.Split(" ");

for (var i = 0; i < words.Length; i++) words[i] =  
Char.ToUpper(words[i][0]).ToString()+words[i].Substring(1);

text = string.Join(" ", words);

我建议使用正则表达式:

 using System.Text.RegularExpressions;

 ...

 string Text = "the sentence which (each!) word must BE capitalized";

 string result = Regex.Replace(Text, 
   @"\b\p{Ll}\p{L}*\b",
    match => {
      string word = match.Value;

      //TODO: Some logic if we want to capitalize the word
      // if (...) return word; 

      return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);  
    }
 );

 Console.Write(result);

结果:

The Sentence Which (Each!) Word Must BE Capitalized

注意,BE 被保留,(each!) 被转换为 (Each!)

模式说明:

 \b     - word boundary 
 \p{Ll} - low case letter
 \p{L}* - zero or more letters
 \b     - word boundary

@Ufuk

如果你期待这个输出“The Sentence Which Each Word Must Be Capitalized”。那么你的代码看起来不错。你已经完成了 95% 的工作。已经与大写分开,但您需要将它们存储在某个地方。 所以我创建了一个字符串变量来存储它们。我只写了两行。

class Program{
  public static void Main (string[] args){
    string Text = "the sentence which each word must be capitalized";
    string outputText="";

    foreach (string Word in Text.Split(' ')){
      string CapitalizedFirstLetter = Word.Substring(0, 1).ToUpper();
      string RestOfWord = Word.Substring(1, Word.Length-1);
      string ConcatenatedWord = string.Concat(CapitalizedFirstLetter, RestOfWord);
      outputText = outputText + " " + ConcatenatedWord ;
    }
  }
}