Convert the entire first letter to uppercase, leave below for each word

I have a line of text (mostly about 5-6 words) that I need to convert.

Currently, the text looks like this:

THIS IS MY TEXT RIGHT NOW 

I want to convert it to:

 THIS IS MY TEXT RIGHT NOW 

I can skip my collection of strings, but I don’t know how to do it.

+106
c # regex
Dec 21 '09 at 23:21
source share
9 answers
 string s = "THIS IS MY TEXT RIGHT NOW"; s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); 
+245
Dec 21 '09 at 23:26
source share

I probably prefer to call ToTitleCase from CultureInfo (System.Globalization) than Thread.CurrentThread (System.Threading)

 string s = "THIS IS MY TEXT RIGHT NOW"; s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); 

but it should be the same as jspcal solution

EDIT

In fact, these solutions are not the same : CurrentThread --calls CurrentThread > CultureInfo !




System.Threading.Thread.CurrentThread.CurrentCulture

 string s = "THIS IS MY TEXT RIGHT NOW"; s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); IL_0000: ldstr "THIS IS MY TEXT RIGHT NOW" IL_0005: stloc.0 // s IL_0006: call System.Threading.Thread.get_CurrentThread IL_000B: callvirt System.Threading.Thread.get_CurrentCulture IL_0010: callvirt System.Globalization.CultureInfo.get_TextInfo IL_0015: ldloc.0 // s IL_0016: callvirt System.String.ToLower IL_001B: callvirt System.Globalization.TextInfo.ToTitleCase IL_0020: stloc.0 // s 



System.Globalization.CultureInfo.CurrentCulture

 string s = "THIS IS MY TEXT RIGHT NOW"; s = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); IL_0000: ldstr "THIS IS MY TEXT RIGHT NOW" IL_0005: stloc.0 // s IL_0006: call System.Globalization.CultureInfo.get_CurrentCulture IL_000B: callvirt System.Globalization.CultureInfo.get_TextInfo IL_0010: ldloc.0 // s IL_0011: callvirt System.String.ToLower IL_0016: callvirt System.Globalization.TextInfo.ToTitleCase IL_001B: stloc.0 // s 



Literature:

+82
May 27 '10 at 8:43 a.m.
source share

There are several ways to convert the first char of a string to uppercase.

The first way is to create a method that simply closes the first char and adds the rest of the string using a substring:

 public string UppercaseFirst(string s) { return char.ToUpper(s[0]) + s.Substring(1); } 

The second way (which is slightly faster) is to split the string into a char array and then rebuild the string:

 public string UppercaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToUpper(a[0]); return new string(a); } 
+16
Dec 21 '09 at 23:27
source share

If you use on a webpage, you can also use CSS:

style="text-transform:capitalize;"

+9
Sep 22 '13 at 21:16
source share

Unconfirmed, but something like this should work:

 var phrase = "THIS IS MY TEXT RIGHT NOW"; var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w"); var newString = rx.Replace(phrase,new MatchEvaluator(m=>m.Value.ToLowerInvariant())); 

Essentially, he says: “prepare a regular expression for all occurrences of an alphanumeric character following another alphanumeric character, and then replace it with the lowercase version of yourself”

+8
Dec 21 '09 at 23:36
source share

When building large tables, speed is a concern, so Jamie Dixon's second function is best, but it doesn't work fully as it is ...

It is not possible to take all letters in lower case, and it uses only the first letter of the string, and not the first letter of each word in the string ... the option below fixes both problems:

  public string UppercaseFirstEach(string s) { char[] a = s.ToLower().ToCharArray(); for (int i = 0; i < a.Count(); i++ ) { a[i] = i == 0 || a[i-1] == ' ' ? char.ToUpper(a[i]) : a[i]; } return new string(a); } 

Although at the moment whether this is still the fastest option is uncertain, the Regex solution provided by George Mauer may be faster ... someone who needs it should test it.

+6
Aug 03 2018-12-12T00:
source share

I do not know if the solution below is more or less effective than the jspcal answer, but I am sure that it requires less object creation than Jamie and George's.

 string s = "THIS IS MY TEXT RIGHT NOW"; StringBuilder sb = new StringBuilder(s.Length); bool capitalize = true; foreach (char c in s) { sb.Append(capitalize ? Char.ToUpper(c) : Char.ToLower(c)); capitalize = !Char.IsLetter(c); } return sb.ToString(); 
+2
Dec 21 '09 at 23:34
source share

In addition to the first answer, do not forget to change the index of the line to select the line to the end of the word, otherwise you will get the reverse order of letters in the line.

 s.SelectionStart=s.Length; 
0
Mar 28 '18 at 14:22
source share

Try this technique; Returns the desired result

 CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower()); 

And don't forget to use System.Globalization .

0
Jul 24. '19 at 19:39
source share



All Articles