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

Time:12-23

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

I was planning to capitalize each words and concatenate it again but, I cannot concatenate it. How should I concatenate it?

CodePudding user response:

you can try this

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

CodePudding user response:

Just in case, you can do it like this:

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

Output:

The Sentence Which Each Word Must Be Capitalized

CodePudding user response:

I suggest using regular expressions:

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

Outcome:

The Sentence Which (Each!) Word Must BE Capitalized

note, that BE is preserved and (each!) is converted into (Each!)

Pattern explained:

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

CodePudding user response:

A slightly inelegant way is to track all the words as you go in a list and then join at the end.

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
  }
}

This is building off the code you posted, and just to show a straightforward way of doing it. It's not necessarily how I would approach this from scratch. At a minimum, I would clean it up and reduce the number of variables in this approach if it were my code.

CodePudding user response:

@Ufuk

If you expect this output "The Sentence Which Each Word Must Be Capitalized". Then your code is looks good. You have already done 95% work. Already splitting with capitalized but you need to store them in some place. So I have created a string variable to store them. Only two lines I have written.

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 ;
    }
  }
} 
  • Related