简体   繁体   English

使用Scanner类解析字符串

[英]Parsing string using the Scanner class

I am trying to parse some lines and check their values, For example in the string: 我试图解析一些行并检查它们的值,例如在字符串中:

   " 1 ON OFF";

I have to check whether: 我必须检查是否:

  • the first character is blank. 第一个字符为空白。
  • the second character is int. 第二个字符是int。
  • the third character is blank. 第三个字符为空白。
  • the fourth character is 2 characters and it is ON. 第四个字符是2个字符,并且为ON。
  • the fifth character is blank. 第五个字符为空白。
  • the sixth character is 3 characters and it is OFF. 第六个字符为3个字符,并且为OFF。

I can do this at one go using regex, but what I want is that after each check I have to display whether is correct or not like: 我可以使用正则表达式一次完成此操作,但是我想要的是在每次检查之后都必须显示是否正确,例如:

   System.out.println("1st character is not a blank : incorrect");
   System.out.println("1st character is blank : correct");

I thought of using Scanner class for this, but when I try to detect the first character, it is showing 1 instead of blank for the string, 我曾考虑为此使用Scanner类,但是当我尝试检测第一个字符时,该字符串显示1而不是空白,

   " 1 ON OFF";

   public class NewClass {    

    public void StringExample(){
        String str = " 1 ON OFF";

        Scanner sc = new Scanner(str);
        System.out.println(sc.next());
    }

    public static void main(String args[]){
        NewClass nc = new NewClass();
        nc.StringExample();
    }
}

Is there any other class in java with which this can be done easily? java中是否还有其他类可以轻松实现?

The closest I can think of is splitting by word boundary: 我能想到的最接近的是按单词边界划分:

String tokens[] = " 1 ON OFF".split("\\b");

It will give the following array: 它将给出以下数组:

{ " ", "1", " ", "ON", " ", "OFF" }

It fits your ordering and your definition of 1st-6th "character". 它符合您的订购要求以及您对第1-6个“字符”的定义。

Try using String.split(" ") , to split around the spaces. 尝试使用String.split(" ")来分割空格。 You'll get an array, and if the first character is blank, the first String in the array will be an empty string: 您将得到一个数组,如果第一个字符为空白,则数组中的第一个String将为空字符串:

String strs[] = " 1 ON OFF".split(" ");

With this example you'll get this array: ["", "1", "ON", "OFF"] . 在此示例中,您将获得以下数组: ["", "1", "ON", "OFF"]

If the first character is a space, you'll get the empty string as the first element. 如果第一个字符是空格,则将空字符串作为第一个元素。 If there are 2 leading spaces, then you'll see empty strings as the first two elements. 如果有2个前导空格,那么您将看到空字符串作为前两个元素。 The remaining strings will be the space separated tokens from the original string, but if there are additional spaces between the tokens then you'll see additional empty strings as array elements. 其余的字符串将是与原始字符串用空格分隔的标记,但是如果标记之间存在其他空格,那么您将看到其他空字符串作为数组元素。

Looping over the resulting array, including a parseInt for the number, you'll be able to match the rules that you've described. 遍历结果数组,包括数字的parseInt ,您将能够匹配所描述的规则。

Note that you can use Scanner to similarly tokenize the string, by setting the delimiter to the empty string: 请注意, 可以通过将分隔符设置为空字符串使用Scanner类似地标记字符串:

Scanner sc = new Scanner(str);
sc.useDelimiter("");
System.out.println(sc.next());

It's worth taking a look at StringReader, which will let you scan through the string character by character. 值得一看的是StringReader,它将使您可以逐字符扫描字符串。 Another option is simply to read each character (String#charAt) and check if it meets your rules. 另一个选择是简单地读取每个字符(String#charAt)并检查它是否符合您的规则。

您可以使用parboiled并声明一个小的语法。

Another alternative : 另一种选择:

public static void main(String[] args) throws IOException
    {

        String patternString = " 1 ON OFF";
        boolean pass = true;

            if (patternString.charAt(0) != ' ' && patternString.charAt(2) != ' ') {
                pass = false;
            }

            int digit = Character.getNumericValue(patternString.charAt(1));

            if (digit < 0 && digit > Integer.MAX_VALUE) {
                pass = false;
            }

            if (patternString.charAt(3) != 'O' && patternString.charAt(4) != 'N') {
                pass = false;
            }

            if (patternString.charAt(5) != ' ' && patternString.charAt(6) != 'O' && patternString.charAt(7) != 'F' && patternString.charAt(8) != 'F') {
                pass = false;
            }

            if (pass) {
                System.out.println("Validation pass");
            }
    }

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

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