How to use names

so basically if i want to convert the name from

stephen smith 

to

 Stephen Smith 

I can easily do this with css on the page, but ideally I would like to catch it earlier and change it when it exits the database. how can i get csharp to capture the string.

is there a function for this?

+44
c #
Feb 20 2018-11-18T00:
source share
8 answers

You can do this using the ToTitleCase method of the ToTitleCase class:

 CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; TextInfo textInfo = cultureInfo.TextInfo; Console.WriteLine(textInfo.ToTitleCase(title)); Console.WriteLine(textInfo.ToLower(title)); Console.WriteLine(textInfo.ToUpper(title)); 
+56
Feb 20 '11 at 15:03
source share
β€” -

The names are complex. Simple first letter rules do not apply . The Senator’s only approach is to ask your users how they want it. Anything else could be a crime.

If my name is MacPhearson, ODowel, or just marc, Marc, or even mArC, then honestly leave it alone. Trust the user so that everything is correct. It gets even more complicated as you walk between cultures.

+24
Feb 20 '11 at 15:39
source share

Hope this helps:) ... But note that the process is likely to be slow if you have many, many lines to change case ...

  string str = "to title case"; Char[] ca = str.ToCharArray(); foreach(Match m in Regex.Matches(str, @"\b[az]")) { ca[m.Index] = Char.ToUpper(ca[m.Index]); } Console.WriteLine(new string(ca)); 

Update:. Or you can also use a custom evaluator to change this type:

  string str = "to title case"; Console.WriteLine(Regex.Replace(str, @"\b[az]", delegate (Match m) { return m.Value.ToUpper(); } )); 

Note that in my test with 1,000,000 iterations, the first method was only 0.48 seconds faster than the one that the evaluator had (the first took 6.88 seconds and the last 7.36 seconds to complete 1000 000 iterations) so I would not take into account to select ...

+4
Feb 20 '11 at 16:32
source share

No no. If you know that the line you are processing is a name (or, rather, a sequence of human names separated by spaces), you should be able to independently encode it in one for loop and using Char.ToUpper . However, there are cultural-specific cases such as the Arabic words bin, al, etc. Used in names that should not be capitalized (using Latin transcription). The same applies to "background" or "van" in Western languages.

Update: Please note that TextInfo.ToTitleCase has a different purpose - it is not intended to smooth out the first letters of human names, but to provide a proper cover for headings (for example, headings of news articles).

Although the current .NET implementation can easily serve the requested purpose, I would not. The reason is that the implementation may change significantly in the future, and therefore it is safer to make an individual implementation for human names. Moreover, I doubt that this method is really applicable for title lines of lines in relation to this culture. For example, in Czech ( "cs-CZ" ), the correct title should use only the first letter of the first word.

+2
Feb 20 '11 at 15:07
source share

A small extension of the answer suggested by Pedro:

 Regex.Replace(Name, @"(?:(M|m)(c)|(\b))([az])", delegate(Match m) { return String.Concat(m.Groups[1].Value.ToUpper(), m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value.ToUpper()); }); 

This will correctly use McNames in addition to the capitalization. e.g. "simon mcguinnis" β†’ "Simon McGuinnis"

  • The first non-capture group will match any interrupt character of the word OR "Mc" / "mc".
  • If it matches the word, then groups 1 and 2 are empty, and group 3 contains this symbol.
  • If it matches β€œMc” or β€œmc,” groups 1 and 2 contain β€œm” and β€œc,” and group 3 is empty.

    • Group 1 (β€œm” or β€œM”) is capitalized.
    • Group 2 ("c") remains unchanged.
    • Group 3 (gap symbol) remains unchanged.
    • Group 4 (the first letter of the next word) is capitalized.

All 4 groups, empty or other, are combined to create a return string.

+1
Dec 04 '14 at 0:29
source share

This works for me with last names that have a character.

  if (Surname.Contains("'")) { String[] Names = Surname.Split('\'').ToArray(); Surname = textInfo.ToTitleCase(Names[0].ToString()); Surname += "''"; Surname += textInfo.ToTitleCase(Names[1].ToString()); } 
+1
Nov 29 '16 at 1:15
source share

This is a string extension method that uses a single word. You can use it together with str.Split() and str.Join to use every word of the str string. You can add checks for strings with a single or single character.

 public static string Capitalize(this string word) { return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower(); } 
0
Feb 08 '17 at 9:41 on
source share

in sight

 string titulo = ""; string result = System.Globalization.CultureInfo.TextInfo.ToLower(titulo); 

then apply the css property

 text-transform = font-family: sans-serif; 
-3
Sep 09 '14 at 15:12
source share



All Articles