Sprache: C#
You need to format an english sentence into a "headline format" (first letter capitalized).
String words = "this is a totally made-up sentence";
Char splitter = ' '
Capitalize(words, splitter);
splitter = '-'
Capitalize(words, splitter);
private static String Capitalize(String words, Char splitter)
{
String[] split;
split = words.Split(splitter);
words = String.Empty;
foreach(String part in split)
{
Char[] chars;
chars = part.ToCharArray();
if(chars.Length > 0)
{
chars[0] = ((new String(chars[0], 1)).ToUpper().ToCharArray())[0];
}
words += new String(chars) + splitter;
}
words = words.Substring(0, words.Length - 1);
return (words);
}
String words = "this is a totally made-up sentence";
Char splitter = ' '
Capitalize(words, splitter);
splitter = '-'
Capitalize(words, splitter);
private static String Capitalize(String words, Char splitter)
{
String[] split;
split = words.Split(splitter);
words = String.Empty;
foreach(String part in split)
{
Char[] chars;
chars = part.ToCharArray();
if(chars.Length > 0)
{
chars[0] = ((new String(chars[0], 1)).ToUpper().ToCharArray())[0];
}
words += new String(chars) + splitter;
}
words = words.Substring(0, words.Length - 1);
return (words);
}
Alte URL:
/snippet/capitalize-the-first-letter-of-every-word/1171
Hi,
concatenation of strings should be done by using the [b]StringBuilder [/b]instead of simple adding strings together.
Just as comment: The framework has a built-in method for this.
[code]
return
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(words);
[/code]
@Günther: Awesome, thx! I looked for something like this, but didn’t find it.
🙂