如何检查字符串是否仅包含某个字符数组中的字母
import java.util.*;
public class Main {
static char[] f = {'q','w','e','r','t','y','u','i','o','p'};
static char[] s = {'a','s','d','f','g','h','j','k','l'};
static char[] t = {'z','x','c','v','b','n','m'};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
for(int i = 0; i < n; i++) {
String str = sc.nextLine();
if(string only contains letters from array f) count++;
}
}
}
最佳答案
这是我的代码,也许可以帮助你:
public static boolean checkIfStringContainsLettersFromArray(String str, char [] arr) {
String arrString = new String(arr);
for(int i = 0; i < str.length(); i++) {
if(arrString.indexOf(str.charAt(i)) < 0) return false;
}
return true;
}
关于java - 如何检查字符串中的字母/字符是否都在字符数组中找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52959987/