我正在尝试从长度超过 10 个字符的字符串中删除元音,除非它们以字符串开头或以单词开头。我不知道如何只删除那些特定的元音。
我尝试过使用 string.replaceAll 以及各种间距组合并尝试分隔元音。我对java非常陌生,不知道还能做什么。我发现完美描述我的问题的唯一其他线程是针对 python 的。
String x = scan.nextLine();
if(x.length() >= 10){
x.toLowerCase();
String y = x.replaceAll("[aeiou]", "");
System.out.println(y);
}else{
System.out.println("This doesn't need shortening!");
如果用户输入“Thequickbrownfoxjumpsovertheextremelylazydog”,则输出应为“Thqckbrwnfxjmpsovrthextrmlylzydg”
最佳答案
我会使用模式 (?<=\S)[aeiou]
在不区分大小写模式下:
String input = "Hello how are you?";
System.out.println(input);
input = input.replaceAll("(?i)(?<=\\S)[aeiou]", "");
System.out.println(input);
打印:
Hello how are you?
Hll hw ar y?
积极的回顾(?<=\S)
断言每个匹配元音之前的内容不是空格。
关于java - 仅删除不是单词开头的元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898908/