简体   繁体   English

替换字符,一输入多字

[英]replace characters, one input multiple words

i have figured out a way to replace vowels into * but it only converts the first line我想出了一种将元音替换为 * 的方法,但它只转换第一行

input: break robert yeah输入:break robert 是的

output: br**k output:br**k

here is the code这是代码

public class Solution {

    public static void main(String[] args) {
        String enterWord;
        Scanner scan = new Scanner (System.in);
        enterWord = scan.nextLine();
        enterWord = enterWord.replaceAll("[aeiou]", "*");
        
    System.out.println(enterWord);
    }
}

is there any way that it reads all three words?有什么办法可以读取所有三个单词?

Your code works as you want (in:break robert yeah out: br**kr*b*rt y**h) on my env(Windows10, java1.8.0_271), maybe you can set a breakpoint on enterWord = enterWord.replaceAll("[aeiou]", "*");你的代码在我的环境(Windows10,java1.8.0_271)上按你想要的方式工作(in:break robert yeah out: br**kr*b*rt y**h),也许你可以在enterWord = enterWord.replaceAll("[aeiou]", "*"); and check is the enterWord recived whole input string.检查是enterWord的整个输入字符串。

You need a loop to keep getting and processing the inputs.您需要一个循环来继续获取和处理输入。 Also, I suggest you use (?i) with the regex to make it case-insensitive .另外,我建议您将(?i)与正则表达式一起使用以使其不区分大小写

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String enterWord, answer = "y";
        Scanner scan = new Scanner(System.in);
        do {
            System.out.print("Enter a word: ");
            enterWord = scan.nextLine();
            enterWord = enterWord.replaceAll("(?i)[aeiou]", "*");
            System.out.println("After replacing vowels with * it becomes " + enterWord);
            System.out.print("Do you wish to conntinue[y/n]: ");
            answer = scan.nextLine();
        } while (answer.equalsIgnoreCase("y"));
    }
}

A sample run:示例运行:

Enter a word: hello
After replacing vowels with * it becomes h*ll*
Do you wish to conntinue[y/n]: y
Enter a word: India
After replacing vowels with * it becomes *nd**
Do you wish to conntinue[y/n]: n

For a single string spanning multiple lines, the method, String#replaceAll works for the entire string as shown below:对于跨越多行的单个字符串,方法String#replaceAll适用于整个字符串,如下所示:

public class Main {
    public static void main(String[] args) {
        String str = "break\n" + 
                    "robert\n" + 
                    "yeah";
        System.out.println(str.replaceAll("(?i)[aeiou]", "*"));
    }
}

Output: Output:

br**k
r*b*rt
y**h

Using this feature, you can build a string of multiple lines interactively and finally change all the vowels to * .使用此功能,您可以交互地构建多行字符串,最后将所有元音更改为*

Demo:演示:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String text = "";
        Scanner scan = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        System.out.println("Keep enter some text (Press Enter without any text to stop): ");
        while (true) {
            text = scan.nextLine();
            if (text.length() > 0) {
                sb.append(text).append(System.lineSeparator());
            } else {
                break;
            }
        }

        System.out.println("Your input: \n" + sb);
        String str = sb.toString().replaceAll("(?i)[aeiou]", "*");
        System.out.println("After converting each vowel to *, your input becomes: \n" + str);
    }
}

A sample run:示例运行:

Keep enter some text (Press Enter without any text to stop): 
break
robert
yeah

Your input: 
break
robert
yeah

After converting each vowel to *, your input becomes: 
br**k
r*b*rt
y**h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM