Java - 不使用 toUppercase() 将小写转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16259250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:02:16  来源:igfitidea点击:

Java - Convert lower to upper case without using toUppercase()

javacharasciiuppercaselowercase

提问by Betty Jones

I'm trying to create a short program that would convert all letters that are uppercase to lowercase (from the command line input).

我正在尝试创建一个简短的程序,它将所有大写字母转换为小写字母(来自命令行输入)。

The following compiles but does not give me the result I am expecting. What would be the reason for this??

以下编译但没有给我我期望的结果。这会是什么原因??

Eg) java toLowerCase BANaNa -> to give an output of banana

例如) java toLowerCase BANaNa -> 给出一个香蕉的输出

 public class toLowerCase{
        public static void main(String[] args){

            toLowerCase(args[0]);
        }

        public static void toLowerCase(String a){

            for (int i = 0; i< a.length(); i++){

                char aChar = a.charAt(i);
                if (65 <= aChar && aChar<=90){
                    aChar = (char)( (aChar + 32) ); 
                }

                System.out.print(a);
            }
         }   
    }

采纳答案by Habib

Looks like homework to me, Just a hint. You are printing string awhereas you are modifying the chartype aChar, its not modifying the original string a. (Remember strings are immutable).

对我来说看起来像作业,只是一个提示。您正在打印字符串,a而您正在修改char类型aChar,而不是修改原始字符串a。(记住字符串是不可变的)。

回答by Akshar Raaj

public static void toLowerCase(String a){

    String newStr = "";

    for (int i = 0; i< a.length(); i++){

        char aChar = a.charAt(i);
        if (65 <= aChar && aChar<=90){
            aChar = (char)( (aChar + 32) ); 
        }
        newStr = newStr + aChar;    
    }
    System.out.println(newStr);
}

You should print newStroutside for loop. You were trying to print it inside the loop

您应该newStr在 for 循环之外打印。你试图在循环内打印它

回答by Rahul Bobhate

You are printing the Stringa, without modifying it. You can print char directly in the loop as follows:

您正在打印Stringa,而不修改它。您可以直接在循环中打印字符,如下所示:

public class toLowerCase
{
    public static void main(String[] args)
    {
        toLowerCase(args[0]);
    }

    public static void toLowerCase(String a)
    {
        for (int i = 0; i< a.length(); i++)
        {
            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<=90)
            {
                aChar = (char)( (aChar + 32) ); 
            }
            System.out.print(aChar);
         }
     }
}    

回答by Robert Bolton

Looks like you're close. :)

看起来你很接近。:)

For starters...

对于初学者...

char aChar = a.charAt(i);

"a" is an array of Strings, so I believe you would want to iterate over each element

“a”是一个字符串数组,所以我相信你会想要遍历每个元素

char aChar = a[i].charAt(0);

and it also seems like you want to return the value of the modified variable, not of "a" which was the originally passed in variable.

并且您似乎还想返回修改后的变量的值,而不是最初传入的变量“a”的值。

System.out.print(aChar);

not

不是

System.out.print(a);

Hope that helps you.

希望能帮到你。

回答by Peter Lawrey

A cleaner way of writing this code is

编写此代码的一种更简洁的方法是

public static void printLowerCase(String a){
    for(char ch: a.toCharArray()) {
       if(ch >= 'A' && ch <= 'Z')
          ch += 'a' - 'A';
       System.out.print(ch);
    }
}

Note: this will not work for upper case characters in any other range. (There are 1,000s of them)

注意:这不适用于任何其他范围内的大写字符。(有 1,000 个)

回答by Yuvaraj Ram

/**
     * Method will convert the Lowercase to uppercase
     * if input is null, null will be returned
     * @param input
     * @return
     */
    public static String toUpperCase(String input){
            if(input == null){
                return input;
            }
            StringBuilder builder = new StringBuilder();
            for(int i=0;i<input.length();i++){
                char stringChar = input.charAt(i);

                if(92 <= stringChar && stringChar <=122){
                    stringChar = (char)( (stringChar - 32) ); 
                    builder.append(stringChar);
                }
                else if (65 <= stringChar && stringChar<=90)
                {
                    builder.append(stringChar);
                }
            }
            if(builder.length() ==0){
                builder.append(input);
            }
            return builder.toString();
        }

回答by Ashish Singh

import java.util.Scanner;
public class LowerToUpperC {

    public static void main(String[] args) {

         char ch;
            int temp;
            Scanner scan = new Scanner(System.in);

            System.out.print("Enter a Character in Lowercase : ");
            ch = scan.next().charAt(0);

            temp = (int) ch;
            temp = temp - 32;
            ch = (char) temp;

            System.out.print("Equivalent Character in Uppercase = " +ch);

    }

}

回答by Hitesh Joshi

public class Changecase
{
    static int i;

    static void changecase(String s)
    {
        for(i=0;i<s.length();i++)
        {
            int ch=s.charAt(i);
            if(ch>64&&ch<91)
            {
                ch=ch+32;
                System.out.print( (char) ch);
            }
            else if(ch>96&&ch<123)
            {
                ch=ch-32;
                System.out.print( (char) ch);
            }
            if(ch==32)
            System.out.print(" ");
        }
    }

    public static void main (String args[])
    {

        System.out.println("Original String is : ");
        System.out.println("Alive is awesome ");
        Changecase.changecase("Alive is awesome ");

    }
}

回答by Pratik Rai

public class MyClass
{
    private String txt;
    private char lower;
    public MyClass(String txt)
    {
        this.txt = txt;
    }
    public void print()
    {
        for(int i=0;i<txt.length();i++)
        {
            if('A' <= txt.charAt(i) && txt.charAt(i) <= 'Z')
            {
                lower = (char)(txt.charAt(i) + 32);
                System.out.print(lower);
            }
            else
            {
                lower = txt.charAt(i);
                System.out.print(lower);
            }
        }
    }
    public static void main(String[] args)
    {
        MyClass mc = new MyClass("BaNaNa");
        mc.print();
    }
}

Sorry pretty late to the scene but this should solve it. An else condition because when it is not zero it totally discards the alphabet.

抱歉到场很晚,但这应该可以解决问题。一个 else 条件,因为当它不为零时,它会完全丢弃字母表。

回答by Chuck

If somebody needs clear code without MagicNumbers and as less as possible conversions here is my solution:

如果有人需要没有 MagicNumbers 的清晰代码和尽可能少的转换,这里是我的解决方案:

final char[] charArray = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
    char c = string.charAt(i);
    charArray[i] = Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c);
}
String.valueOf(charArray);