[help]how find a word in a string ?

Discussion in 'Plugin Development' started by jordanske, Jan 20, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    jordanske

    Code:
    hey.
    i got a little problem.
    i have a file with:
    test=text1;blabla;anotherthing;morestuff
    inside it
    how can i check if "blabla" is in the test.
    i searching a whole day and im just a beginner :p
    thank you
    i got what i want:
    Code:
    final List<String> words = Arrays.asList(string.split(";"));
    if (words.contains("Woowoo!")) {
        // Woowoo! code here
    }
    if (words.contains("Blah!")) {
        // Blah! code here
    }
    
    this thread name can be set to solved
     
  2. Offline

    blaatz0r

    Code:
    if (test.contains("blabla") {
       // do stuff
    } else {
       // do other stuff
    }
    Check the Java API ;)
     
  3. Offline

    jordanske

    that not what i want ,
    Code:
    test=text1;blabla;anotherthing;morestuff
    if i do
    if (test.contains("blabla") {
    it works but
    if (test.contains("bla") {
    works to
    i want it to chest from ; to ;
    but thanks for trying helping
     
  4. Offline

    Archelaus

    You want it to chest? I don't understand what that means.
     
  5. Offline

    jordanske

    sry it need to be check, typo
     
  6. Offline

    MadMichi

    Maybe you are looking for something like
    Code:
    public class StringSplit {
      public static void main(String args[]) throws Exception{
        String testString = "Real-How-To";
        System.out.println(
            java.util.Arrays.toString(
            testString.split("-")
        ));
        // output : [Real, How, To]
        }
    }
    
     
  7. Offline

    Archelaus

    Actually, I think I might understand.

    Code:
    String[] teststring = "test=text1;blabla;anotherthing;morestuff".split(";");
    if(teststring[1].contains("bla") {
    //stuff to do
    }
     
  8. Offline

    Fifteen

    The most simple way should be:
    Code:
    if (thisstring.contains("blabla;"))
    {
    
    }
    
    You would have to add a ; at the end of the last "word", though. And there are better ways to do this.
     
  9. Offline

    Affecting_Nelas

    a test of the exact word, so bla and blabla makes a diffrence, can't use contains my solution would be:


    String teststring = "test=text1;blabla;anotherthing;morestuff".split(";");
    for (String iter: teststring)
    // ------------------------------
    if(iter.equals("bla") {
    //stuff to do
    }
    // ------------------------------ or
    if(iter == "bla") {
    //stuff to do
    }
     
  10. Offline

    MysticX

    The solution of MadMichi and Affecting_Nelas is the best I guess. But consider using equalsIgnoreCase(), depending of what you want to do ( for example player name matching..)
     
  11. Offline

    jordanske

    Ok thanks i will try it tomorrow got to sleep now.
     
  12. Offline

    Afforess

    How about this:

    String str
    if (str.indexOf("Cool") > -1) {
    //do stuff
    }

    indexOf returns a zero or above value when the sub string is found, -1 when not.
     
  13. Offline

    MysticX

    He said in #3 that he doesn't want to match substrings (indexOf("Cool") would return an index for the "Cool" substring of "CoolGuy22") , otherwise it would be perfectly fine :)
     
  14. Offline

    DjDCH

    The wise man has spoken.
     
  15. Offline

    Afforess

    I see. The OP needs to be clearer then. ;)
     
  16. Offline

    Mixcoatl

    I would do this:
    Code:
    final List<String> words = Arrays.asList(string.split(";"));
    if (words.contains("Woowoo!")) {
        // Woowoo! code here
    }
    if (words.contains("Blah!")) {
        // Blah! code here
    }
     
  17. Offline

    jordanske

    Yes this one worked thanks :D
     
Thread Status:
Not open for further replies.

Share This Page