如何修复:字符串中每个单词的首字母大写 - 除了一个字母单词
How to Fix: Uppercase first letter of every word in the string - except for one letter words
我有一个代码可以将每个单词的第一个字母大写,除了一个字母单词。
我遇到的问题是,如果该字符串的最后一个单词是一个字母,它会给出索引超出范围的异常。这是有道理的,因为最后一个字母的代码 array[i + 1]
不存在。
static string UppercaseWords(string value)
{
char[] array = value.ToLower().ToCharArray();
// Handle the first letter in the string.
array[0] = char.ToUpper(array[0]);
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ' && array[i + 1] != ' ')
{
array[i] = char.ToUpper(array[i]);
}
}
return new string(array);
}
我只是在寻找解决该异常的方法,或其他方法。
您可以改善条件,这样您就不会要求第 (i+1) 个索引。
if (array[i - 1] == ' ' && i + 1 < array.Length && array[i + 1] != ' ')
建议:
我会为您的字符串处理做一个扩展 class:
public static class StringExtensions
{
public static string AllWordsInStringToUpperCase(this string value)
{
return string.Join(' ', value.Split(' ').Select(s => s.FirstCharToUpperCase()));
}
public static string FirstCharToUpperCase(this string word)
{
if (word.Length == 1)
return word;
return word.First().ToString().ToUpper() + word.Substring(1);
}
}
然后在控制台应用程序中使用它作为示例:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("fuu a bar".AllWordsInStringToUpperCase());
}
}
输出:Fuu a Bar
这样你就可以编写一些测试,这样你就知道你得到了你想要的行为。
和平!
您可以提取所有单词(字符串部分用白色分隔space),当部分长度为> 1
:
string input = "this is a sample, string with: some => 1 letter words ! a";
StringBuilder sb = new StringBuilder(input.Length * 2);
foreach (string word in input.Split())
{
if (word.Length > 1) {
sb.Append(char.ToUpper(word[0]));
sb.Append(word.Substring(1));
}
else {
sb.Append(word);
}
sb.Append((char)32);
}
Console.WriteLine(sb);
打印:
This Is a Sample, String With: Some => 1 Letter Words ! a
我有一个代码可以将每个单词的第一个字母大写,除了一个字母单词。
我遇到的问题是,如果该字符串的最后一个单词是一个字母,它会给出索引超出范围的异常。这是有道理的,因为最后一个字母的代码 array[i + 1]
不存在。
static string UppercaseWords(string value)
{
char[] array = value.ToLower().ToCharArray();
// Handle the first letter in the string.
array[0] = char.ToUpper(array[0]);
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ' && array[i + 1] != ' ')
{
array[i] = char.ToUpper(array[i]);
}
}
return new string(array);
}
我只是在寻找解决该异常的方法,或其他方法。
您可以改善条件,这样您就不会要求第 (i+1) 个索引。
if (array[i - 1] == ' ' && i + 1 < array.Length && array[i + 1] != ' ')
建议:
我会为您的字符串处理做一个扩展 class:
public static class StringExtensions
{
public static string AllWordsInStringToUpperCase(this string value)
{
return string.Join(' ', value.Split(' ').Select(s => s.FirstCharToUpperCase()));
}
public static string FirstCharToUpperCase(this string word)
{
if (word.Length == 1)
return word;
return word.First().ToString().ToUpper() + word.Substring(1);
}
}
然后在控制台应用程序中使用它作为示例:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("fuu a bar".AllWordsInStringToUpperCase());
}
}
输出:Fuu a Bar
这样你就可以编写一些测试,这样你就知道你得到了你想要的行为。
和平!
您可以提取所有单词(字符串部分用白色分隔space),当部分长度为> 1
:
string input = "this is a sample, string with: some => 1 letter words ! a";
StringBuilder sb = new StringBuilder(input.Length * 2);
foreach (string word in input.Split())
{
if (word.Length > 1) {
sb.Append(char.ToUpper(word[0]));
sb.Append(word.Substring(1));
}
else {
sb.Append(word);
}
sb.Append((char)32);
}
Console.WriteLine(sb);
打印:
This Is a Sample, String With: Some => 1 Letter Words ! a