John R
John R

Reputation: 3026

Parse a string in Java

I have strings formatted similar to the one below in a Java program. I need to get the number out.

Host is up (0.0020s latency).

I need the number between the '(' and the 's' characters. E.g., I would need the 0.0020 in this example.

Upvotes: 2

Views: 246

Answers (6)

CassOnMars
CassOnMars

Reputation: 6181

Sounds like a case for regular expressions.

You'll want to match for the decimal figure and then parse that match:

Float matchedValue;
Pattern pattern = Pattern.compile("\\d*\\.\\d+");
Matcher matcher = pattern.matcher(yourString);
boolean isfound = matcher.find();

if (isfound) {
    matchedValue = Float.valueOf(matcher.group(0));
}

Upvotes: 3

Sergey Gazaryan
Sergey Gazaryan

Reputation: 1043

Of course using regular expressions in this case is best solution but in many simple cases you can use also something like :

String value = myString.subString(myString.indexOf("("), myString.lastIndexOf("s")) double numericValue = Double.parseDouble(value);

This is not recomended because text in myString can changes.

Upvotes: 0

volk
volk

Reputation: 1196

This is a great site for building regular expressions from simple to very complex. You choose the language and boom.

http://txt2re.com/

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 837926

If you are sure it will always be the first number you could use the regular expresion \d+\.\d+ (but note that the backslashes need to be escaped in Java string literals).

Try this code:

String input = "Host is up (0.0020s latency).";
Pattern pattern = Pattern.compile("\\d+\\.\\d+");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    System.out.println(matcher.group());
}

See it working online: ideone

You could also include some of the surrounding characters in the regular expression to reduce the risk of matching the wrong number. To do exactly as you requested in the question (i.e. matching between ( and s) use this regular expression:

\((\d+\.\d+)s

See it working online: ideone

Upvotes: 3

Nick Rolando
Nick Rolando

Reputation: 26157

Here's a way without regex

    String str = "Host is up (0.0020s latency).";
    str = str.substring(str.indexOf('(')+1, str.indexOf("s l"));
    System.out.println(str);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

It depends on how "similar" you mean. You could potentially use a regular expression:

import java.math.BigDecimal;
import java.util.regex.*;

public class Test {
    public static void main(String args[]) throws Exception {
        Pattern pattern = Pattern.compile("[^(]*\\(([0-9]*\\.[0-9]*)s");        
        String text = "Host is up (0.0020s latency).";

        Matcher match = pattern.matcher(text);
        if (match.lookingAt())
        {
            String group = match.group(1);
            System.out.println("Before parsing: " + group);
            BigDecimal value = new BigDecimal(group);
            System.out.println("Parsed: " + value);
        }
        else
        {
            System.out.println("No match");
        }
    }
}

Quite how specific you want to make your pattern is up to you, of course. This only checks for digits, a dot, then digits after an opening bracket and before an s. You may need to refine it to make the dot optional etc.

Upvotes: 2

Related Questions