[solved] Evaluate string

Discussion in 'Spigot Plugin Development' started by aFreshKiwi, Jun 15, 2015.

  1. Really quick question I can't seem to work out;

    Imagine a message used in a chat event so for example

    Hi @aFreshKiwi how are you

    For example i'd use e.getMessage() on an AsyncPlayerChatEvent to get that string, but then how would I go about determining if there is an @<word> in there and then get the value of the word after the @ symbol?

    Thanks

    edit resolved - found a way to do it
     
    #1 aFreshKiwi, Jun 15, 2015
    Last edited: Jun 15, 2015
  2. clip

    Benefactor

    Check if the message contains the space character:

    String msg = event.getMessage();

    If msg.contains(" ") {

    String[] words = msg.split (" ");

    for (String word : words) {

    if (word.startsWith("@') {
    // do stuff
    }
    }
    }

    On my phone so forget making it look nice but you should get it.
     
  3. You have several options to this, either the above, regex or scanning the text a character at a time.
     
  4. A regex with a positive lookbehind for @ shold do the trick. Something like
    Code (Text):
    You can then use a matcher to find and retrieve the results as strings.

    EDIT: Obviously you would need to escape the backslash properly.