如何小写字母转换为大写字母和大写字母为小写字母(how to convert Lower case

2019-08-17 01:35发布

或者显示在文本框中键入任何文本

//     in either Capital or lowercase depending on the original
//     letter changed.  For example:  CoMpUtEr will convert to
//     cOmPuTeR and vice versa.
    Switch.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e )

            String characters = (SecondTextField.getText()); //String to read the user input
            int length = characters.length();  //change the string characters to length

         for(int i = 0; i < length; i++)  //to check the characters of string..
         {             
            char character = characters.charAt(i);          

            if(Character.isUpperCase(character)) 
            {
                SecondTextField.setText("" + characters.toLowerCase());

            }
            else  if(Character.isLowerCase(character))
            {
                 SecondTextField.setText("" + characters.toUpperCase()); //problem is here, how can i track the character which i already change above, means lowerCase**
                }               
         }}     
    });

Answer 1:

setText正在改变文本内容,你给它到底是什么,不附加它。

转换的String从外地第一,然后直接将其...

String value = "This Is A Test";
StringBuilder sb = new StringBuilder(value);
for (int index = 0; index < sb.length(); index++) {
    char c = sb.charAt(index);
    if (Character.isLowerCase(c)) {
        sb.setCharAt(index, Character.toUpperCase(c));
    } else {
        sb.setCharAt(index, Character.toLowerCase(c));
    }
}

SecondTextField.setText(sb.toString());


Answer 2:

您不必跟踪您是否已经改变了性格从上到下。 您的代码已经在这样做,因为它基本上是:

1   for each character x:
2       if x is uppercase:
3           convert x to lowercase
4       else:
5           if x is lowercase:
6                convert x to uppercase.

你有一个事实, else在那里(4号线),意味着最初大写字符绝不会在第二个检查if语句(第5行)。

例如,开始与A 。 因为这是大写,它将会被转换为小写在第3行,然后你就回去了1号线下一个字符。

如果你开始z时, if在2号线将直接送你到5号线在那里将被转换为大写。 凡是是既不上也不是小写会失败if陈述,因此保持不变。



Answer 3:

您可以使用StringUtils.swapCase()org.apache.commons



Answer 4:

这是一个更好的方法: -

void main()throws IOException
{
    System.out.println("Enter sentence");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    String sentence = "";
    for(int i=0;i<str.length();i++)
    {
        if(Character.isUpperCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)+32);
            sentence = sentence + ch2;
        }
        else if(Character.isLowerCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)-32);
            sentence = sentence + ch2;
        }
        else
        sentence= sentence + str.charAt(i);

    }
    System.out.println(sentence);
}


Answer 5:

问题是,您要设置的值SecondTextField检查原始字符串的每一个字符之后。 你应该这样做“就在身边”的转换,一个字符的时间,才把设置结果到SecondTextField

当你经历了原始的字符串,开始撰写从一个空字符串输出。 请追加字符在相反的情况下,直到你用完的字符。 一旦输出就绪,将其设置成SecondTextField

您可以输出一个String ,将其设置为空字符串"" ,和你去追加角色吧。 这将工作,但是这是一个低效的方法。 更好的办法是使用一个StringBuilder类,它可以让你改变的字符串,不扔掉整个事情。



Answer 6:

String name = "Vikash";
String upperCase = name.toUpperCase();
String lowerCase = name.toLowerCase();


Answer 7:

这是不使用任何字符串函数的更好方法。

public static String ReverseCases(String str) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
      char temp;
      if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
        temp = (char)(str.charAt(i) - 32);
      }
      else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
        temp = (char)(str.charAt(i) + 32);
      }
      else {
        temp = str.charAt(i);
      }

      sb.append(temp);
    }
    return sb.toString();
  }


Answer 8:

在这里,您有一些其他的版本:

公共类回文{

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a word to check: ");
    String checkWord = sc.nextLine();
    System.out.println(isPalindrome(checkWord));
    sc.close();

}

public static boolean isPalindrome(String str) {        
    StringBuilder secondSB = new StringBuilder();
    StringBuilder sb = new StringBuilder();
    sb.append(str);
    for(int i = 0; i<sb.length();i++){
        char c = sb.charAt(i);
        if(Character.isUpperCase(c)){
            sb.setCharAt(i, Character.toLowerCase(c));
        }
    }
    secondSB.append(sb);
    return sb.toString().equals(secondSB.reverse().toString());
}

}



Answer 9:

StringBuilder b = new StringBuilder();

Scanner s = new Scanner(System.in);
String n = s.nextLine();

for(int i = 0; i < n.length(); i++) {
    char c = n.charAt(i);

    if(Character.isLowerCase(c) == true) {
        b.append(String.valueOf(c).toUpperCase());
    }
    else {
        b.append(String.valueOf(c).toLowerCase());
    }
}

System.out.println(b);


Answer 10:

方法描述:

*toLowerCase()* Returns a new string with all characters converted to lowercase.

*toUpperCase()* Returns a new string with all characters converted to uppercase.

例如:

"Welcome".toLowerCase()返回一个新字符串, 欢迎

"Welcome".toUpperCase()返回一个新字符串, 欢迎



Answer 11:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String satr=scanner.nextLine();
    String newString = "";
    for (int i = 0; i < satr.length(); i++) {
        if (Character.isUpperCase(satr.charAt(i))) {
            newString+=Character.toLowerCase(satr.charAt(i));
        }else newString += Character.toUpperCase(satr.charAt(i));
    }
    System.out.println(newString);
}


Answer 12:

public class Toggle {
public static String toggle(String s) {
    char[] ch = s.toCharArray();

    for (int i = 0; i < s.length(); i++) {
        char charat = ch[i];
        if (Character.isUpperCase(charat)) {
            charat = Character.toLowerCase(charat);
        } else
            charat = Character.toUpperCase(charat);
        System.out.print(charat);
    }
    return s;
  }

public static void main(String[] args) {
    toggle("DivYa");
   }
  }


Answer 13:

import java.util.Scanner;
class TestClass {
    public static void main(String args[]) throws Exception {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (Character.isUpperCase(ch[i])) {
                ch[i] = Character.toLowerCase(ch[i]);
            } else {
                ch[i] = Character.toUpperCase(ch[i]);
            }
        }
        System.out.println(ch);
    }
}


Answer 14:

//This is to convert a letter from upper case to lower case
import java.util.Scanner;
    public class ChangeCase {
        public static void main(String[]args) {

            String input;
            Scanner sc= new Scanner(System.in);
                System.out.println("Enter Letter from upper case");
                input=sc.next();

            String result;
            result= input.toLowerCase();
            System.out.println(result);
        }
    }


Answer 15:

    String str1,str2;
    Scanner S=new Scanner(System.in);
    str1=S.nextLine();
    System.out.println(str1);
    str2=S.nextLine();
    str1=str1.concat(str2);
    System.out.println(str1.toLowerCase()); 


文章来源: how to convert Lower case letters to upper case letters & and upper case letters to lower case letters