Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Quickest way to check if a string contains a double value

I am reading from many large text files and I have to check if each snippet of text contains a double value or not. The regex code I am currently using is causing my program to run very slowly because in total I am checking 10 billion strings. I know that due to the large number of strings that I am checking, my program is bound to run slowly. But is there a more efficient and quicker way to check if a String is a double value, thus decreasing the runtime of the program? Thanks

if (string[i].matches(".*\\d.*")) {

.....
}

Also, the strings from the text file are read into an array before the I check them, therefore time isn't wasted constantly reading the text file.

like image 907
Matt9Atkins Avatar asked Jan 18 '26 11:01

Matt9Atkins


1 Answers

Use the Pattern and Matcher classes:

public static final Pattern DOUBLE = Pattern.compile("\\d");

...

if (DOUBLE.matcher(string[i]).find()) {
    ...
}
like image 92
arshajii Avatar answered Jan 21 '26 03:01

arshajii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!