如何使字符串中每个单词的第一个或最后一个字母小写

时间:2019-10-02 21:29:43

标签: c# string char uppercase lowercase

假设我们传递的字符串包含多个单词。

是否可以将字符串中每个单词的第一个或最后一个字母变为小写或大写?

我尝试了文本信息类,但是它只为每个第一个字符提供了大写方法。

我真的想不起来如何硬编码自己的方法。

2 个答案:

答案 0 :(得分:2)

您可以使用这些扩展方法来放入一个静态类,例如StringHelper

    using System.Linq;
    static public string LastLetterOfWordsToLower(this string str)
    {
      if ( str == null ) return null;
      var words = str.Split(' ');
      for ( int indexWord = 0; indexWord < words.Length; indexWord++ )
      {
        string word = words[indexWord];
        if ( word != "" )
        {
          for ( int indexChar = word.Length - 1; indexChar >= 0; indexChar-- )
            if ( char.IsLetter(word[indexChar]) )
            {
              char c = char.ToLower(word[indexChar]);
              words[indexWord] = word.Substring(0, indexChar) + c;
              if ( indexChar != word.Length - 1 )
                words[indexWord] += word.Substring(indexChar + 1);
              break;
            }
        }
      }
      return string.Join(" ", words);
    }
    static public string FirstLetterOfWordsToLower(this string str)
    {
      if ( str == null ) return null;
      var words = str.Split(' ');
      for ( int indexWord = 0; indexWord < words.Length; indexWord++ )
      {
        string word = words[indexWord];
        if ( word != "" )
        {
          for ( int indexChar = 0; indexChar < word.Length; indexChar++ )
            if ( char.IsLetter(word[indexChar]) )
            {
              char c = char.ToLower(word[indexChar]);
              words[indexWord] = c + word.Substring(indexChar + 1);
              if ( indexChar != 0 )
                words[indexWord] = word.Substring(0, indexChar) + words[indexWord];
              break;
            }
        }
      }
      return string.Join(" ", words);
    }

测试:

    static public void StringHelperTest()
    {
      string[] list =
      {
        null,
        "",
        "A",
        "TEST",
        "A TEST STRING,  FOR STACK OVERFLOW!!"
      };
      foreach ( string str in list )
        Console.WriteLine(str.LastLetterOfWordsToLower());
      foreach ( string str in list )
        Console.WriteLine(str.FirstLetterOfWordsToLower());
    }

输出:



a
TESt
A TESt STRINg,  FOr STACk OVERFLOw!!


a
tEST
a tEST sTRING,  fOR sTACK oVERFLOW!!

StringBuilder可用于性能问题。

答案 1 :(得分:-1)

有很多方法可以做到这一点。我建议您使用ToCharArray方法来获取字符数组。然后,您可以遍历字符并确定哪个字符位于单词的第一个和最后一个字符,并更改字母的大小写。这样,您只需执行一次遍历,就不会进行任何字符串构建或连接。

这里有一些示例方法,涵盖了两种情况以及将第一个和最后一个字符设置得较低的组合方法。对于大写字母,您只需将ToLower调用替换为对ToUpper的调用。

private static string FirstLetterOfWordToLowercase(string stringToTransform)
{
   char[] stringCharacters = stringToTransform.ToCharArray();
   for (int charIndex = 0; charIndex < stringCharacters.Length; charIndex++)
   {
      char currentCharacter = stringCharacters[charIndex];
      if (!isWordSepparator(currentCharacter))
      {
         if (charIndex == 0 || (charIndex > 0 && isWordSepparator(stringCharacters[charIndex - 1])))
         {
            stringCharacters[charIndex] = char.ToLower(currentCharacter);
         }
      }
   }
   return new string(stringCharacters);
}

private static string LastLetterOfWordToLowercase(string stringToTransform)
{
   char[] stringCharacters = stringToTransform.ToCharArray();
   for (int charIndex = 0; charIndex < stringCharacters.Length; charIndex++)
   {
      char currentCharacter = stringCharacters[charIndex];
      if (!isWordSepparator(currentCharacter))
      {
         if (charIndex == stringCharacters.Length - 1 || isWordSepparator(stringCharacters[charIndex + 1]))
         {
            stringCharacters[charIndex] = char.ToLower(currentCharacter);
         }
      }
   }
   return new string(stringCharacters);
}

private static string FirstAndLastLetterOfWordToLowercase(string stringToTransform)
{
   char[] stringCharacters = stringToTransform.ToCharArray();
   for (int charIndex = 0; charIndex < stringCharacters.Length; charIndex++)
   {
      char currentCharacter = stringCharacters[charIndex];
      if (!isWordSepparator(currentCharacter))
      {
         if (charIndex == 0 || charIndex == stringCharacters.Length - 1 // is first or last character
            || (charIndex > 0 && isWordSepparator(stringCharacters[charIndex - 1])) // previous character was a word separator
            || isWordSepparator(stringCharacters[charIndex + 1])) // next character is a word separator
         {
            stringCharacters[charIndex] = char.ToLower(currentCharacter);
         }
      }
   }
   return new string(stringCharacters);
}

private static bool isWordSepparator(char character)
{
   return char.IsPunctuation(character) || char.IsSeparator(character);
}

这里也是指向.NET Fiddle的链接,您可以在其中看到它的工作状态。