Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading