Java – compares two integer strings and prints a match

Let's say these are my two strings

String listOfIntegers = ("1 5 9 12 15 50 80 121");
String integerToLookFor = ("12");

I want my program to scan listofintegers and print out whether integertolookfor is in a string Any ideas?

Solution

>Splits a string with spaces into separators to get an array of strings

Code:

String listOfIntegers = ("1 5 9 12 15 50 80 121");
String integerToLookFor = ("12");
String[] splitArr = listOfIntegers.split("\\s");
for(String s: splitArr){
    if(s.equals(integerToLookFor)) {
        System.out.println("found: " + s);
        break; //breaks out of the loop
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>