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

问题描述 投票:-1回答:2

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

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

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

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

c# string char uppercase lowercase
2个回答
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;
            }
        }
      }
      str = "";
      foreach ( string word in words )
      {
        if ( str != "" ) str += " ";
        str += word;
      }
      return str;
    }
    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;
            }
        }
      }
      str = "";
      foreach ( string word in words )
      {
        if ( str != "" ) str += " ";
        str += word;
      }
      return str;
    }

测试:

    static public void StringHelperTest()
    {
      string str1 = null;
      string str2 = "";
      string str3 = "A";
      string str4 = "TEST";
      string str5 = "A TEST STRING,  FOR STACK OVERFLOW!!";
      Console.WriteLine(str1.LastLetterOfWordsToLower());
      Console.WriteLine(str2.LastLetterOfWordsToLower());
      Console.WriteLine(str3.LastLetterOfWordsToLower());
      Console.WriteLine(str4.LastLetterOfWordsToLower());
      Console.WriteLine(str5.LastLetterOfWordsToLower());
      Console.WriteLine(str1.FirstLetterOfWordsToLower());
      Console.WriteLine(str2.FirstLetterOfWordsToLower());
      Console.WriteLine(str3.FirstLetterOfWordsToLower());
      Console.WriteLine(str4.FirstLetterOfWordsToLower());
      Console.WriteLine(str5.FirstLetterOfWordsToLower());
    }

输出:



a
TESt
A TESt STRINg,  FOr STACk OVERFLOw!!


a
tEST
a tEST sTRING,  fOR sTACK oVERFLOW!!

StringBuilder可以用于性能问题。


-1
投票

有很多方法可以做到这一点。我建议您使用ToCharArray方法来获取字符数组。然后,您可以使用索引来更改所需字符的大小写。更改之后,将其转换回字符串。

这里是将第一个和最后一个字符设置为小写的示例:

string stringToTransform = "AN UPERCASE TEST STRING";
char[] stringCharacters = stringToTransform.ToCharArray();
stringCharacters[0] = Char.ToLower(stringCharacters[0]); // first character
stringCharacters[stringCharacters.Length - 1] = Char.ToLower(stringCharacters[stringCharacters.Length - 1]); // last character
string transformedString = new string(stringCharacters);

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

© www.soinside.com 2019 - 2024. All rights reserved.