0

使用 for 循环,如何使字符串中的所有辅音都大写?

我想我应该做这样的事情:

    String str = "fish$"     

    String strConsonants = "f, s, h"; 

    for (int x = 0; x < str.length(); x++) 
    {
       if(((str.charAt(x) == (strConsonants))
       {
          System.out.print("FiSH$");
       }
    }
4

5 回答 5

1

使用String API中的 String.contains() 方法。以下代码适用于您当前的输入。通常,如果您想查找所有辅音,请使用 char[] 的辅音或带有所有辅音的字符串并进行检查。

String str = "fish$";     
String strConsonants = "f, s, h"; 
String temp="";
for (int x = 0; x < str.length(); x++){
temp+= str.charAt(x);   
   if(!strConsonants.contains(temp)) {
    consonentsUCase+=temp.toUpperCase();
   }
   temp="";
}
于 2012-09-29T22:20:41.983 回答
1

我刚刚写好了。

输出:鱼$

适用于任何单词!;)

接口方法: printStringWithUpperConsonant

   import java.util.HashSet;
   import java.util.Set;

   public class ConsonantUtils {

        private Set<Character> vowels = getVowels();

        private String analysedString;

        public ConsonantUtils(String analysedString) {
            this.analysedString = analysedString;
        }

        public static void main(String[] args) {
            new ConsonantUtils("fish$").printStringWithUpperConsonant();
        }

        public void printStringWithUpperConsonant() {
            for (int i = 0; i < getAnalysedString().length(); i++) {
                printChar(getCurrentChar(i));
            }
        }

        private char getCurrentChar(int i) {
            return getAnalysedString().charAt(i);
        }

        private void printChar(char currentChar) {
            if (isConsonant(currentChar)) {
                System.out.print(makeCharUpperCase(currentChar));
            }
            else{
                System.out.print(currentChar);
            }
        }

        private Set<Character> getVowels() {
            Set<Character> vowels = new HashSet<Character>();
            vowels.add('a');
            vowels.add('e');
            vowels.add('i');
            vowels.add('o');
            vowels.add('u');
            return vowels;
        }

        private char makeCharUpperCase(char character) {
            return Character.toUpperCase(character);
        }

        private boolean isConsonant(char currentChar) {
            return !vowels.contains(currentChar);
        }

        private String getAnalysedString(){
            return analysedString;
        }
    }
于 2012-09-29T22:41:06.043 回答
0

您可以使用Character.toUpperCase().

String vowels = "aeiouAEIOU";
char[] charArr = str.toCharArray(); //turn the string into char array
for(int i=0; i<charArr.length; i++) { //for loop
    if(vowels.indexOf(charArr[i]) == -1) { // if not a vowel
        charArr[i] = Character.toUpperCase(charArr[i]); //replace with upper case
    }
}
Sting rslt = String.valueOf(charArr); //finally turn the array back to string
于 2012-09-29T22:20:56.437 回答
0
String str = "fish$";   
String strVowels = "aeiouAEIOU";
String out = "";

for (Character c : str.toCharArray()) 
{
    if(!strVowels.contains(""+c))
    {
        out = out + Character.toUpperCase(c);
    }
    else
    {
        out = out + c;
    }
}
System.out.println(out);
于 2012-09-29T22:42:42.160 回答
0

这对我有用:

     List<Character> constants = Arrays.asList('f', 's', 'h');
     String str = "fish$";

     for (Character c : constants) {             
           str = str.replace(c, Character.toUpperCase(c));
      }
于 2012-09-29T23:02:19.763 回答