Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop to search for word in string

Tags:

java

I can't seem to find the syntax needed for the for loop in this method. I am looking to iterate through the words in the string suit.

EDIT: one thing to note is that cardArray is a ArrayList.

public String getSuit(int card){
    String suit = cardArray.get(card);
    for (String word : suit){
        if (word.contains("SPADES")){
            suit = "SPADES";            
        }
    }
    return suit;
}
like image 905
Bob Avatar asked Mar 18 '13 15:03

Bob


1 Answers

You could use

for (String word : suit.split(" ")) {

to split on every space character (U+0020).

Alternatively:

for (String word : suit.split("\\s+")) {

This splits on every sequence of whitespace character (this includes tabs, newlines etc).

like image 115
NPE Avatar answered Oct 04 '22 10:10

NPE