Java中替换字符串中的字符

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

Java replace a character in a string

问题

  1. public Boolean isVowel(char ch){
  2. char ch2 = Character.toLowerCase(ch);
  3. char[] vowels = {'a', 'e', 'i', 'o', 'u'};
  4. for(int i = 0; i < vowels.length; i++){
  5. if (ch2 == vowels[i]) {
  6. return true;
  7. }
  8. }
  9. return false;
  10. }
  11. public String replaceVowels(String phrase, char ch){
  12. String newP = "";
  13. for(int i = 0; i < phrase.length(); i++){
  14. char c = phrase.charAt(i);
  15. Boolean vowel = isVowel(c);
  16. if(vowel){
  17. newP = phrase.replace(c, ch);
  18. }
  19. }
  20. return newP;
  21. }
英文:

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:

  1. public Boolean isVowel(char ch){
  2. char ch2 = Character.toLowerCase(ch);
  3. char[] vowels = {&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;};
  4. for(int i = 0; i &lt; vowels.length; i++){
  5. if (ch2 == vowels[i]) {
  6. return true;
  7. }
  8. }
  9. return false;
  10. }
  11. public String replaceVowels(String phrase, char ch){
  12. String newP = &quot;&quot;;
  13. for(int i = 0; i &lt; phrase.length(); i++){
  14. char c = phrase.charAt(i);
  15. Boolean vowel = isVowel(c);
  16. if(vowel){
  17. newP = phrase.replace(c, ch);
  18. }
  19. }
  20. return newP;
  21. }

答案1

得分: 6

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

答案2

得分: 2

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

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

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

  1. String str = &quot;hEllo&quot;;
  2. char ch = &#39;X&#39;;
  3. 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.

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

Could also be more explicit with case like:

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

答案3

得分: 1

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

  1. String newP = phrase;
  2. for(int i = 0; i < phrase.length(); i++){
  3. char c = phrase.charAt(i);
  4. Boolean vowel = isVowel(c);
  5. if(vowel){
  6. newP = newP.replace(c, ch);
  7. }
  8. }

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

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

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

  1. String newP = phrase;
  2. for(int i = 0; i &lt; phrase.length(); i++){
  3. char c = phrase.charAt(i);
  4. Boolean vowel = isVowel(c);
  5. if(vowel){
  6. newP = newP.replace(c, ch);
  7. }
  8. }

Better solution as answered by Alex just add String,

  1. 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:

确定