Avatar billede holmqvist Nybegynder
08. oktober 2003 - 17:51 Der er 14 kommentarer og
1 løsning

Searching Strings

How do you search strings in Java?
ex: String x = "I like apples" ;
If I want to search this string to find out wheter it contains the word "apple" (or apples if its not possible to search parts of words..)How do I do that?
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 17:53 #1
Yes, you'll have to look at the StringTokenizer Class in the API.
Example is comming..
Avatar billede simonvalter Praktikant
08. oktober 2003 - 18:01 #2
can't you use this?

x.indexOf("apple");

if it returns -1 it dosen't contain the word
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 18:03 #3
public class TestMe {
    public static void main(String[] args) {
        String test = "I like apples";
        java.util.StringTokenizer st = new java.util.StringTokenizer(test, " ");
        while (st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }

    }
}
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 18:06 #4
Oh yes, you can also use indexOf().. Depends on what your needs are..
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 20:52 #5
StringTokenizer seems good.. there's the nextToken and so that works really sweet. But How do I do "thisToken"? (already tried "this" of course :) )
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 20:58 #6
I'm not sure I understand.. Maybe I'm dumb ;-) ??

By the way....

public class TestMe {
    public static void main(String[] args) {
        String test = "I like apples";
        java.util.StringTokenizer st = new java.util.StringTokenizer(test);
        while (st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }

    }
}

You can actually simplify the code a bit.. A different constructor for StringTokenizer.

Feel free to look at http://java.sun.com/j2se/1.4.2/docs/api/index.html
It explains quite a lot about the above mentioned class.
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 21:18 #7
I was probably fussy..
I mean that when I loop the StringTokenizer, nextToken works great.. but after I found what I wanted, I don't want nextToken, I want the present token.. If I use nextToken again I will get the word after the one I want.
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 21:19 #8
Ohh, perhaps by "thisToken" you mean the current token; that is, the current token in the string that you are scanning. What you are trying to do is to scan the current token/string to see if is contains e.g. apple?
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 21:23 #9
Yes. That's it!
how?
Avatar billede arne_v Ekspert
08. oktober 2003 - 21:24 #10
nextToken starter før første token og returnerer derfor første token første gang
den kaldes !
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 21:28 #11
Yees.. i know.. but when I've already used nextToken in the loop then I can't reach it again when I want to use it.. because then it will be the next after the one i want to use...........
Avatar billede arne_v Ekspert
08. oktober 2003 - 21:30 #12
Gem en referance.

Eller (forudsat at du bruger J2SE 1.4) brug split.

Den returnerer String[].
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 21:31 #13
Like this:
public class TestMe {

    public static void main(String[] args) {
        String test = "I like apples";
        String searchedWord = "apples";
        String token=null;
        boolean endLoop = true;
        java.util.StringTokenizer st = new java.util.StringTokenizer(test);

        while (st.hasMoreTokens() && endLoop) {
            if (searchedWord.equals(token=st.nextToken())) {
                System.out.println(token);
                endLoop = false;
            }
        }
    }
}
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 21:39 #14
ooh.. *clever*
That was some great help!
You really deserved your points magoo.
Avatar billede magoo20000 Nybegynder
08. oktober 2003 - 21:44 #15
No problem.. StringTokenizer is pretty neat.. You can also use regular expressions if you need to match Strings(>=JDK 1.4). It's a bit harder to undertstand but it's worth looking at!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester