如何使用 .contains 来 select 某些字母
how to use .contains to select certain letters of the alphabet
我需要用户输入:
- 没有号码
- 长度为 4 个字符
- 仅使用字母表中的某些字母 [R, B, G, P, Y, O]
我已经想出如何不做任何数字和只有 4 个字符的长度,但是,我似乎无法弄清楚如何限制字母表中的某些字母(除了 R、B、G、P、Y、O 以外的所有字母。)
guess = input.nextLine();
guess = guess.toUpperCase();
while (guess.length() != 4 || guess.contains("[0-9]") || guess.contains("[ACDEFHIJKLMNQSTUVWXZ]")) {
System.out.println("Bad input! Try again");
System.out.println("Use the form \"BGRY\"");
guess = input.nextLine();
}
这是我目前的代码,它似乎不起作用
while(!guess.matches("[RBGPYO]{4}")) {
// ...
}
演示:
public class Main {
public static void main(String s[]) {
// Tests
System.out.println(matches("RBGPYO"));
System.out.println(matches("RBGP"));
System.out.println(matches("R1BGP"));
System.out.println(matches("ABCD"));
System.out.println(matches("1234"));
System.out.println(matches("BGPY"));
System.out.println(matches("BYPG"));
}
static boolean matches(String input) {
return input.matches("[RBGPYO]{4}");
}
}
输出:
false
true
false
false
false
true
true
不用Regex的方法也有很多
您不能使用 String::contains
in this case since this method works with a certain character sequence and your use-case is too specific. However, you can use the advantage of List::contains
which might be more useful as long as the String
is be understood as a List<Character>
using java-stream:
List<Integer> characters = "RBGPYO".chars()
.boxed()
.collect(Collectors.toList());
boolean matches = guess.length() == 4 &&
guess.toUpperCase().chars().allMatch(characters::contains);
如果您不喜欢这个功能,一个很好的老循环方法:
List<Character> characters = Arrays.asList('R', 'B', 'G', 'P', 'Y', 'O');
boolean matches = guess.length() == 4;
if (matches) {
for (char ch : guess.toUpperCase().toCharArray()) {
if (!characters.contains(ch)) {
matches = false;
break; // it's important to break the cycle
}
}
}
无论如何,重要的是在检查字符之前检查长度。只要 guess
应该包含字符并且具有 一定的 长度,这就是有效的。
我需要用户输入:
- 没有号码
- 长度为 4 个字符
- 仅使用字母表中的某些字母 [R, B, G, P, Y, O]
我已经想出如何不做任何数字和只有 4 个字符的长度,但是,我似乎无法弄清楚如何限制字母表中的某些字母(除了 R、B、G、P、Y、O 以外的所有字母。)
guess = input.nextLine();
guess = guess.toUpperCase();
while (guess.length() != 4 || guess.contains("[0-9]") || guess.contains("[ACDEFHIJKLMNQSTUVWXZ]")) {
System.out.println("Bad input! Try again");
System.out.println("Use the form \"BGRY\"");
guess = input.nextLine();
}
这是我目前的代码,它似乎不起作用
while(!guess.matches("[RBGPYO]{4}")) {
// ...
}
演示:
public class Main {
public static void main(String s[]) {
// Tests
System.out.println(matches("RBGPYO"));
System.out.println(matches("RBGP"));
System.out.println(matches("R1BGP"));
System.out.println(matches("ABCD"));
System.out.println(matches("1234"));
System.out.println(matches("BGPY"));
System.out.println(matches("BYPG"));
}
static boolean matches(String input) {
return input.matches("[RBGPYO]{4}");
}
}
输出:
false
true
false
false
false
true
true
不用Regex的方法也有很多
您不能使用 String::contains
in this case since this method works with a certain character sequence and your use-case is too specific. However, you can use the advantage of List::contains
which might be more useful as long as the String
is be understood as a List<Character>
using java-stream:
List<Integer> characters = "RBGPYO".chars()
.boxed()
.collect(Collectors.toList());
boolean matches = guess.length() == 4 &&
guess.toUpperCase().chars().allMatch(characters::contains);
如果您不喜欢这个功能,一个很好的老循环方法:
List<Character> characters = Arrays.asList('R', 'B', 'G', 'P', 'Y', 'O');
boolean matches = guess.length() == 4;
if (matches) {
for (char ch : guess.toUpperCase().toCharArray()) {
if (!characters.contains(ch)) {
matches = false;
break; // it's important to break the cycle
}
}
}
无论如何,重要的是在检查字符之前检查长度。只要 guess
应该包含字符并且具有 一定的 长度,这就是有效的。