字符串转换小编程

题目1:将大写字母转为小写字母,将小写转为大写

方法1

 str.toUpperCase();
//        str.toLowerCase();

方法2

(char)(arr[i]-32)

(char)(arr[i]+32)

a97
A65
0 48

方法一完整代码

package com.sxy.javabase;

import java.util.Scanner;

public class StringTset1 {
    public static void main(String[] args) {
//        String str="helloHELLOhelp";
//        str.toUpperCase();
//        str.toLowerCase();
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String str=sc.nextLine();
            char[] arr=str.toCharArray();
            for(int i=0;i='a'&&arr[i]<'z'){
                    System.out.print(String.valueOf(arr[i]).toUpperCase());
                }else
                if(arr[i]>='A'&&arr[i]<'Z'){
                    System.out.print(String.valueOf(arr[i]).toLowerCase());
                }else {
                    System.out.print(arr[i]);
                }
            }
        }
    }
}

方法2

package com.sxy.javabase;

import java.util.Scanner;

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

        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String str=sc.nextLine();
            char[] arr=str.toCharArray();
            for(int i=0;i='a'&&arr[i]<'z'){//A65 a97 0 48
                    System.out.print((char)(arr[i]-32));
//                    System.out.print(String.valueOf(arr[i]).toUpperCase());
                }else
                if(arr[i]>='A'&&arr[i]<'Z'){
                    System.out.print((char)(arr[i]+32));
//                    System.out.print(String.valueOf(arr[i]).toLowerCase());
                }else {
                    System.out.print(arr[i]);
                }
            }
        }
    }
}

字符串自测

//        String str="helloHELLOhelp";
//        str.toUpperCase();
//        str.toLowerCase();
        System.out.println('a'+1);//98
        System.out.println('a'+'1');//146 97+49=146
        System.out.println('a');
        System.out.println('A'+0);//65
        System.out.println((char)('A'+32));//a

参考
https://blog.csdn.net/AriesTina/article/details/102654129

你可能感兴趣的:(字符串转换小编程)