Get a String between two Strings?

Discussion in 'Plugin Development' started by Uniclaw, Dec 18, 2012.

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

    Uniclaw

    Hi!

    If i have a thing like:

    Hello %wWorld%, how are you?

    How, can i get "World" ? I've tryed to scan for the first % token, and then split the rest.. but yeah, bad idea ._. How can i get a this thing, without nowing the lentgh? (If i now the length i can make substring or a thig like that..)

    Greet
     
  2. Offline

    evilmidget38

    Find the first % token, substring, then find the second one. Then substring the original one again to get the string between the two tokens.
     
  3. Offline

    Bench3

    you could just convert that string into a char array and set the pointer to 2-6 to be added to a new string using StringBuilder?
     
  4. Offline

    ImDeJay

    might also be easier to know what your trying to accompish?

    where is %wWorld% being read from?
    What is %wWorld% replacing?
     
  5. Offline

    Tirelessly

    string.replaceAll("%World%", "people")
     
  6. Offline

    nisovin

    Splitting on % and getting the first element is probably easiest and simplest. If you want something more advanced, say you want to get multiple strings out of a single string, you could use regular expressions.

    Code:
            String s = "test %world% another test %wow% awesome";
            Matcher matcher = Pattern.compile("%(.*?)%").matcher(s);
            while (matcher.find()) {
                System.out.println(matcher.group(1));
            }
    
     
  7. Offline

    Uniclaw

    @To all wo answered this thread Thanks :D ! Works now :) Just a little question: what is doing mather.group()?
     
Thread Status:
Not open for further replies.

Share This Page