Remove specific characters from string in Java

前端 未结 3 1151
孤城傲影
孤城傲影 2021-02-20 00:28

For example, given a string of Battle of the Vowels:Hawaii vs Gronzy when we specify the characters to be removed as aeiou, the function should transfo

3条回答
  •  灰色年华
    2021-02-20 00:44

    public class RemoveChars {
    
        char[] replaceChar = {'a','e','i','o','u'};
    
        public static void main(String[] args) {
            String src = "Battle of the Vowels:Hawaii vs Gronzy";
            System.out.println(new RemoveChars().removeChar(src));
        }
    
        public String removeChar(String src){
            char[] srcArr = src.toCharArray(); 
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < srcArr.length; i++) {
                char foundChar = isFound(srcArr[i]);
                if(foundChar!='\0')
                sb.append(foundChar);           
            }
            return sb.toString();
    
        } 
    
        public char isFound(char src){      
            for (int i = 0; i < replaceChar.length; i++) {
                if(src==replaceChar[i]){
                    return '\0';
                }
            }
            return src;
        }
    }
    

提交回复
热议问题