如何在java程序中找到字符串中的元音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19066582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find vowels in a string in java program
提问by user2128433
What is the java coding to find out vowels in a string? How to use OR operator in java? because the symbol " || " is not taken while i executing the java program.
找出字符串中元音的java编码是什么?如何在java中使用OR运算符?因为在我执行 java 程序时没有使用符号“ || ”。
采纳答案by upog
try the below code The program iterats over the given string and check if each character is an vowel 1. The symbol '||' can be used as OR, below program is an example
试试下面的代码程序迭代给定的字符串并检查每个字符是否是元音 1. 符号“||” 可以用作 OR,下面的程序是一个例子
public class Test {
public static void main(String[] args) {
String str ="This is a test";
for(int i=0;i <str.length();i++){
if((str.charAt(i) == 'a') ||
(str.charAt(i) == 'e') ||
(str.charAt(i) == 'i') ||
(str.charAt(i) == 'o') ||
(str.charAt(i) == 'u')) {
System.out.println(" The String contains " + str.charAt(i));
}
}
}
}
Note: it will match only lower case vowels
注意:它只会匹配小写元音
回答by Ajeesh
Stringclass has the containsmethod which can be used to find out whether a Stringvalue have vowels or not. It also has matchesmethod which can be used to match characters using regular expressions.
String类具有contains方法,可用于确定String值是否具有元音。它还具有匹配方法,可用于使用正则表达式匹配字符。