How to recover integers?

问题: I get a string and I have to retrieve the values Je pense que nous devons utiliser le ".slit" if (stringReceived.contains("ID")&& stringReceived.contains("Value...

问题:

I get a string and I have to retrieve the values

Je pense que nous devons utiliser le ".slit"

if (stringReceived.contains("ID")&& stringReceived.contains("Value")) {

here is my character string: [11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475

I would like to recover the value of Timestamp, Id and Value..


回答1:

You can also use regex for that. Something like:

 String example="[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";          
        Pattern pattern=Pattern.compile(".*Timestamp=(\d+).*ID=(\d+).*Value=(\d+)");
        Matcher matcher = pattern.matcher(example);
        while(matcher.find()) {
            System.out.println("Timestamp is:" + matcher.group(1));
            System.out.println("Id is:" + matcher.group(2));
            System.out.println("Value is:" + matcher.group(3));
        }

If the order of tokens can be different (for example ID can come before Timestamp) you can also do it. But since it looks like log which is probably structured I doubt you will need to.


回答2:

First [11/2/19 9:48:25] seems unnecessary so let's remove it by jumping right into "Timestamp".

Using indexOf(), we can find where Timestamp starts.

// "Timestamp=1549878505 ID=4 Value=2475"
line = line.substring(line.indexOf("Timestamp")); 

Since each string is separated by space, we can split it.

// ["Timestamp=1549878505", "ID=4" ,"Value=2475"]
line.split(" ");

Now for each tokens, we can substring it using index of '=' and parse it into string.

for(String token: line.split(" ")) {
    int v = Integer.parseInt(token.substring(token.indexOf('=') + 1));
    System.out.println(v);
}

Hope that helps :)


回答3:

A simple regex is also an option:

private int fromString(String data, String key) {
    Pattern pattern = Pattern.compile(key + "=(\d*)");
    Matcher matcher = pattern.matcher(data);
    if (matcher.find()) {
        return Integer.parseInt(matcher.group(1));
    }
    return -1;
}

private void test(String data, String key) {
    System.out.println(key + " = " + fromString(data, key));
}

private void test() {
    String test = "[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";
    test(test, "Timestamp");
    test(test, "ID");
    test(test, "Value");
}

prints:

Timestamp = 1549878505

ID = 4

Value = 2475


回答4:

You can try that:

        String txt= "[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475";
        String re1= ".*?\d+.*?\d+.*?\d+.*?\d+.*?\d+.*?\d+.*?(\d+).*?(\d+).*?(\d+)";
        Pattern p = Pattern.compile(re1,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m = p.matcher(txt);
        if (m.find())
        {
            String int1=m.group(1);
            String int2=m.group(2);
            String int3=m.group(3);
            System.out.print("("+int1+")"+"("+int2+")"+"("+int3+")"+"n");
        }

回答5:

String text = "Timestamp=1549878505 ID=4 Value=2475";
        Pattern p = Pattern.compile("ID=(\d)");
        Matcher m = p.matcher(text);
        if (m.find()) {
            System.out.println(m.group(1));
        }

output 4


回答6:

Use below code, You will find your timestamp at index 0, id at 1 and value at 2 in List.

    Pattern pattern = Pattern.compile("=\d+");
    Matcher matcher = pattern.matcher(stringToMatch);

    final List<String> matches = new ArrayList<>();
    while (matcher.find()) {
        String ans = matcher.group(0);
        matches.add(ans.substring(1, ans.length()));
    }

Explaining the regex

= matches the character = literally

d* matches a digit (equal to [0-9])

* Quantifier — Matches between zero and unlimited times, as many times as possible

  • 发表于 2019-02-13 17:44
  • 阅读 ( 171 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除