for循环查找字符串中的辅音

时间:2012-09-29 22:18:36

标签: java string for-loop

使用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$");
       }
    }

5 个答案:

答案 0 :(得分:1)

使用String API中的String.contains()方法。以下代码适用于您目前的输入。通常情况下,如果你想找到所有的组成部分,请拥有一个char [] of consonents或String with all consonents并进行检查。

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="";
}

答案 1 :(得分:1)

我刚刚写完了。

输出:FiSH $

适用于任何单词! ;)

API方法: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;
        }
    }

答案 2 :(得分: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

答案 3 :(得分: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);

答案 4 :(得分:0)

这对我有用:

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

     for (Character c : constants) {             
           str = str.replace(c, Character.toUpperCase(c));
      }