将字母转换为小写而不使用toLowerCase()

时间:2018-04-30 02:31:23

标签: java for-loop

我正在尝试将字母转换为小写,我正在使用for循环而不使用.tolowerCase。我不确定但它不起作用......这是我的代码..

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String a1 = "apple";
    char[] s1 = a1.toCharArray();  

    for (int i=0; i<s1.length; i++)
    {
        int CharNumber = s1[i] - 'a';
        int NewChar = (char) CharNumber;
        char[] NewNew = Character.toChars(NewChar);
        System.out.println(NewNew);

    }

}

3 个答案:

答案 0 :(得分:4)

您可以简单地将每个字符视为整数,并将32加到任何ASCII整数值小于或等于90的字符。这会将大写字母转换为小写字母,并保留小写字母。

String a1 = "aPplE";
char[] s1 = a1.toCharArray();
int diff = 'a' - 'A';

for (int i=0; i < s1.length; i++) {
    if (s1[i] >= 'A' && s1[i] <= 'Z') {
        s1[i] += diff;
    }
}

System.out.println(s1);

apple

Demo

答案 1 :(得分:0)

找到一种有效的方法,可能会有所帮助。请参阅此http://www.java2s.com/Code/Java/Data-Type/RewrotethetoLowercasemethodtoimproveperformances.htm

public class Main {

  private static final char[] LOWER_CASE =
      { 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0, '-',   0,   0, 
          '0', '1', '2', '3', '4', '5', '6', '7', 
          '8', '9',   0,   0,   0,   0,   0,   0, 
            0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
          'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
          'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
          'x', 'y', 'z',   0,   0,   0,   0,   0, 
            0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
          'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
          'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
          'x', 'y', 'z',   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0 
      };

  public static final String toLowerCase( String value )
  {
      char[] chars = value.toCharArray();

      for ( int i = 0; i < chars.length; i++ )
      {
          chars[i] = LOWER_CASE[ chars[i]];
      }

      return new String( chars );
  }

}

答案 2 :(得分:0)

这应该有效:

     private static String  toLowerCase(String inputWord)
          { 

        StringBuilder inputW = new StringBuilder(inputWord);

        for(int i = 0 ; i < inputW.length() ; i++)
            { 
         // check for Capital  letter ...   ascii code of "A" is 65 ... "Z" is 91.
            if(inputW.charAt(i) >= 65 && inputW.charAt(i) <=91)   
                {
                 //   The character at the specified index is set to  to uppercase
                    inputW.setCharAt(i, (char)(inputW.charAt(i)+32));    
                }
        }
        return inputW.toString();
    }