简体   繁体   English

如何检查字符串是否等于有效字符之一? (国际象棋棋盘)

[英]How to check if a string equals to one of valid characters? (Chess Board)

So I want to check if the entered position is valid in Chess Board, 所以我想检查输入的位置在国际象棋棋盘中是否有效,

//returns true if the position is in the range of A1-H8

private boolean isValid(String position){
    char first=position.charAt(0);
    String letter=(""+first).toLowerCase();
    boolean validLetter=position.equals("a") || position.equals("b") || position.equals("c")|| 
            position.equals("d") || position.equals("e")|| position.equals("f") || position.equals("g") ||
            position.equals("h");

As you can see the boolean is pretty ugly, so what is a better way of doing this? 如您所见,布尔值非常丑陋,那么有什么更好的方法呢?

By the way, how do you check if the second character is a number?? 顺便问一下,如何检查第二个字符是否为数字?

===edit==== Thanks everyone! ===编辑====谢谢大家! But all your answers seem so complicated to me and I just start learning java, so would you please give me a more fundamental approach to the problem? 但是您的所有答案对我来说似乎都太复杂了,我才开始学习Java,所以请您给我更基本的解决方法?

A regular expression makes short work of this is one: 正则表达式可以简化以下内容:

private boolean isValid(String position) {
    return position.matches("^[a-h][1-8]$");
}

If you you're not comfortable with regexes, maybe something like this will suit you better: 如果您对正则表达式不满意,也许像这样更适合您:

private boolean isValid(String position) {
    if (position.length() != 2) {
        return false;
    }
    char firstChar = position.charAt(0);
    List<Character> validFirstChars = Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
    if (!validFirstChars.contains(firstChar)) {
        return false;
    }
    char secondChar = position.charAt(1);
    List<Character> validSecondChars = Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8');
    if (!validSecondChars.contains(secondChar)) {
        return false;
    }
    return true;
}

As an optimization, validFirstChars and validSecondChars could be made final static members of the class instead of instantiated on each method invocation. 作为一种优化,可以使validFirstCharsvalidSecondChars成为类的final static成员,而不是在每次方法调用时实例化它们。

Characters are really integers in Java, which can be used to check if a character falls within a certain range. 字符实际上是Java中的整数,可用于检查字符是否在一定范围内。 So you can check if the character falls within the range 97 to 104 (which is the values of 'a' to 'h' in ascii ): 因此,您可以检查字符是否在97到104的范围内(这是ascii中'a'到'h'的值):

return first < 105 && first > 96;

(You can read more about characters on my page here :) (您可以在我的页面上阅读有关字符的更多信息:)

A really simple example : 一个非常简单的例子:

private boolean isValid(String position) {
  // We will check only strings with 2 characters
  if (position.length() != 2) {
    return false;
  }
  // If the first char is not between a and h, let's return false.
  char first = position.charAt(0);
  if (first < 'a' || 'h' < first) {
    return false;
  }
  // If the second char is not between 1 and 8, let's return false.
  char second = position.charAt(1);
  if (second < '1' || '8' < second) {
    return false;
  }
  // We couldn't prove the position was invalid, so it must be valid.
  return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM