从字符串中选择大写字母并将其转换为字符数组

时间:2014-11-04 08:48:04

标签: java string

如何将字符串转换为字符数组但有一点扭曲:只应将大写字母放入字符数组中。

例如

  • 输入字符串"abCcAB123"
  • 输出字符数组{C,A,B}

4 个答案:

答案 0 :(得分:1)

删除所有非大写字母,然后转换为char数组。

String str = "abCcAB";
String a = str.replaceAll( "[^A-Z]", "" );
System.out.println(a.toCharArray());

答案 1 :(得分:1)

你可以这样做:

public static void main(String[] args) {
        String s="ab !@#$&*()//CcAB897987";
         List upperChar=new ArrayList();
         String a = s.replaceAll( "[^A-Z\\-]", "" );  // remove all character other Uppercase character
         char []ch=a.toCharArray();              // this contains all uppercase character
         for (int i = 0; i < ch.length; i++) {
               upperChar.add(ch[i]);             // make a list of uppercase char.
         }

           System.out.println(upperChar);

 }

输出:

[C, A, B]

答案 2 :(得分:0)

psedocode:

    queue output; //declaration of the character array for output
    while(input.length!=0){ //checking the length of the input array
        firstcharacter=string.charAt(0); //getting the firstCharacter of the input string
        if(character.IsUpperCase(firtcharacter)){ //checking if the firstCharacter is uppercase
            output.enqueue(firtcharacter); //if the character is uppercase add to the array
        }
        input=input.substring(1,string.length); //remove the first character from the string
    }
//output should now contain all the uppercase characters.

这是解决问题的逻辑。

答案 3 :(得分:0)

试试这个:

public class CaptialChars {
    public static void main(String[] args){
        String input="SaGaR";
        char[] output=input.replaceAll("[a-z]", "").toCharArray();
        for(char character:output){
            System.out.println(character);
        }

    }
}