Solved .trim() not working?

Discussion in 'Plugin Development' started by Cheesepro, Dec 14, 2014.

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

    Cheesepro

    .trim() not working? I don't know why... is it something because of the chat formatting thing in Bukkit?
    Code:
        @EventHandler
        public void noAdsmsg(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            if(!p.hasPermission("CheeseEss.chat.ads")) {
                String link = e.getMessage().toLowerCase().trim();
                p.sendMessage(link);
                if (link.contains(".com") || link.contains(".net") || link.contains(".me") || link.contains(".tk") || link.contains(".org") || link.contains(".us") || link.contains(".eu") || link.contains(".ca") || link.contains(".biz") || link.contains(".co") || link.contains(".info")) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.YELLOW.toString() + ChatColor.BOLD + "!==================================!");
                    p.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "###!!!!!Link(s) detected in message!!!!!###");
                    p.sendMessage(ChatColor.RED.toString() + ChatColor.BOLD + " *Messages sending action cancelled!*");
                    p.sendMessage(ChatColor.YELLOW.toString() + ChatColor.BOLD + "!==================================!");
                }
            }
        }
     
  2. Offline

    mythbusterma

    @Cheesepro

    Considering you haven't described the desired behaviour or the observed behaviour, we can't help you.

    However, you are accessing configs asynchronously, which is a bad idea.
     
  3. Offline

    teej107

     
  4. Offline

    Funergy

    @Cheesepro Split the string with a white space. this will return a collection of strings
    ( String[] ) and then check if a string in the collection of strings starts with www
     
  5. Offline

    Cheesepro

    @Funergy @teej107 @mythbusterma
    Thank you guys all, but the only reason I want to no space is to prevent some advertisers type out website link like "google . com" or "g o o g l e . com"

    And p.sendMessage(link); in the code prints out ". com" when I say ". com" in chat, since I think it should be printed like ".com"

    SOLVED!!
    apparently .replaceAll(" ", "") works... for some reason

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  6. Offline

    teej107

    trim() just removes the whitespaces in the start and end of the string as I have quoted from the documentation.
     
  7. Offline

    Cheesepro

    @teej107
    Ohhhh s*** I forgot about dat xD
     
  8. replaceAll() is for when using regex - since you're not using regex, I recommend that you change it to replace(). It will still replace all occurrences. :)
     
    es359 likes this.
Thread Status:
Not open for further replies.

Share This Page