3

Got a Question:

How can i combine message.equalsIgnoreCase and message.startswith() ?

i.e:

if (message.startsWith("!spoiler")) {
    String name = sender;
    if (!name.equalsIgnoreCase(ownerchannel)){
        try {
            String spoiler = message.split("!spoiler ")[1];
            sendMessage(channel, "/timeout "+name+" 1");
            if(englishmode == true){
                sendMessage(channel, "Spoiler from "+name+" deleted. Click at 'message deleted' above to read it!");
            }else{
                sendMessage(channel, "Spoiler von "+name+" wurde zensiert. Wer diesen lesen möchte klickt einfach oben auf 'message deleted'.");
            }
        } catch (Exception e) {
        }
    }
}

in my code above, !spoiler xyz will trigger it but !Spoiler xyz wont. how can i combine it to ma tch startswith + ignorecase?

user3220962
  • 371
  • 1
  • 7
  • 19

4 Answers4

5

You could use toLowerCase

if (message.toLowerCase().startsWith("!spoiler")) {
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • And this wouldn't set the whole message to lowercase when i output the "message"-string? – user3220962 Apr 02 '14 at 18:44
  • you're not changing the message String itself since `toLowerCase` returns a new `String` (Strings are immutable) – Reimeus Apr 02 '14 at 18:45
  • This answer is wrong. If you look at the implementation of `String.equalsIgnoreCase()` you will discover that you need to compare **both** lowercase and uppercase versions of the Strings before you can conclusively return `false`. See http://stackoverflow.com/a/38947571/14731 for an alternative answer. – Gili Aug 14 '16 at 23:36
0

Convert (a copy of) the string to all lower-case, then just use .startsWith() with the lower-case version of what it's checking for.

Mar
  • 7,765
  • 9
  • 48
  • 82
0

split requires a regular expression. You would have to use the regular expression for the case you don't care about.

message.split("![Ss]poiler");
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Use the following code to combine both:

if (message.startsWith("!spoiler") && !sender.equalsIgnoreCase(ownerchannel)) {
        String name = sender;
            try {
                String spoiler = message.split("!spoiler ")[1];
                sendMessage(channel, "/timeout "+name+" 1");
                if(englishmode == true){
                    sendMessage(channel, "Spoiler from "+name+" deleted. Click at 'message deleted' above to read it!");
                }else{
                    sendMessage(channel, "Spoiler von "+name+" wurde zensiert. Wer diesen lesen möchte klickt einfach oben auf 'message deleted'.");
                }
            } catch (Exception e) {
            }

    }