Java中替换字符串中的字符

huangapple go评论96阅读模式
英文:

Java replace a character in a string

问题

public Boolean isVowel(char ch){
    char ch2 = Character.toLowerCase(ch);
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    
    for(int i = 0; i < vowels.length; i++){
        if (ch2 == vowels[i]) {
            return true;
        }
    }
    return false;
}

public String replaceVowels(String phrase, char ch){
    String newP = "";
    for(int i = 0; i < phrase.length(); i++){  
        char c = phrase.charAt(i);
        Boolean vowel = isVowel(c);
        
        if(vowel){ 
           newP = phrase.replace(c, ch);
        }
    }
    return newP;
}
英文:

I am writing a method to replace all vowels in a string with a given character, but it does not work for strings with more than one vowel. It works for "heel" but not for "hello". Please help. My code below:

public Boolean isVowel(char ch){
        
        char ch2 = Character.toLowerCase(ch); 
        char[] vowels = {&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;};
        
        for(int i = 0; i &lt; vowels.length; i++){
            if (ch2 == vowels[i]) {
                return true;
            }
        }
            return false;
    }
    
    public String replaceVowels(String phrase, char ch){
        String newP = &quot;&quot;;
        for(int i = 0; i &lt; phrase.length(); i++){  
            char c = phrase.charAt(i);
            Boolean vowel = isVowel(c);
            
            if(vowel){ 
               newP = phrase.replace(c, ch);
            }
        }
        
        return newP;
    }

答案1

得分: 6

public String replaceVowels(final String phrase, final String ch) {
    return phrase.replaceAll("[aeiou]", ch);
}
英文:
public String replaceVowels(final String phrase,final String ch) {
    return phrase.replaceAll(&quot;[aeiou]&quot;, ch);
}

答案2

得分: 2

这是一种用 Java 字符替换字符串中所有元音字母的方法。其中的 (?i) 表示不区分大小写。"" +ch 从字符中获取一个字符串。

String str = &quot;hEllo&quot;;
char ch = &#39;X&#39;;
str = str.replaceAll( &quot;(?i)[aeiou]&quot;, &quot;&quot; +ch );

也可以更明确地区分大小写,如下:

String str = &quot;hEllo&quot;;
char ch = &#39;X&#39;;
str = str.replaceAll( &quot;[aeiouAEIOU]&quot;, &quot;&quot; +ch );
英文:

Here is one way to replace all vowels in a string with an java char. The (?i) is to make it case insensitive. The "" +ch gets a string from the char.

String str = &quot;hEllo&quot;;
char ch = &#39;X&#39;;
str = str.replaceAll( &quot;(?i)[aeiou]&quot;, &quot;&quot; +ch );

Could also be more explicit with case like:

String str = &quot;hEllo&quot;;
char ch = &#39;X&#39;;
str = str.replaceAll( &quot;[aeiouAEIOU]&quot;, &quot;&quot; +ch );

答案3

得分: 1

你的问题在于 newP = phrase.replace(c, ch); 你正在赋予一个最终值。

String newP = phrase;
        for(int i = 0; i < phrase.length(); i++){
            char c = phrase.charAt(i);
            Boolean vowel = isVowel(c);

            if(vowel){
                newP = newP.replace(c, ch);
            }
        }

根据Alex的回答,更好的解决方案是添加 String:

phrase.replaceAll("[aeiou]", "" + ch);
英文:

Your problem is newP = phrase.replace(c, ch); You are assigning a last value.

String newP = phrase;
        for(int i = 0; i &lt; phrase.length(); i++){
            char c = phrase.charAt(i);
            Boolean vowel = isVowel(c);

            if(vowel){
                newP = newP.replace(c, ch);
            }
        }

Better solution as answered by Alex just add String,

phrase.replaceAll(&quot;[aeiou]&quot;, &quot;&quot;+ch);

huangapple
  • 本文由 发表于 2020年4月4日 20:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61028144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定