Java 如果字符串结尾有元音,比如(aioeuAIOEU),我该怎么办
使用正则表达式检查:Java 如果字符串结尾有元音,比如(aioeuAIOEU),我该怎么办,java,Java,使用正则表达式检查: import java.util.Scanner; public class String5 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("cumleni daxil et:");
import java.util.Scanner;
public class String5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("cumleni daxil et:");
String a = sc.nextLine();
char [] b= new char [20];
if (a.endsWith("aioeuAIOEU")) {
a.getChars(1, 10, b, 0);
System.out.println(b);
如果a中的文本以任意aeiouAEIOU结尾,则为真。您可以执行相反的操作,并检查AEIOUYAEOUY是否包含字符串的最后一个字母:
if (a.matches("^.*[aeiouAEIOU]$"))
这里有一些东西可能与你正在尝试的是一致的。 和其他人一样,通过将最后一个字符与一组固定的小写和大写元音进行比较,更容易发现最后一个字符是否是元音之一
if ( "aeiouyAEIOUY".contains(a.substring(s.length()-1, a.length())))
我不是那样写的a.endsWitha | | | a.endsWithe | | | | a.endsWitho……….请编辑您的问题以添加描述,一个清晰的问题,这样其他人可以更快地帮助您。您正在检查您的字符串是否以完整的aioeuAIOEU结尾,而不是任何一个字母。是否希望字符串的最后一个字符是元音??如果是,那么您可以这样做字符串str=您的字符串;AEIOUaeiou.indexOfstr.substringstr.length-1>0;正则表达式不应该是[aeiouAEIOU]$?
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Test4 {
private static final List<Character> vowels = Arrays.asList('a','i','o','e','u','A','E','I','O','U');
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
if (vowels.contains(a.charAt(a.length()-1))){
System.out.println("Last character is a vowel");
}
}
}