Bukkit trim 不工作

Bukkit trim not working

出于某种奇怪的原因,当我尝试 trim "replace:" 时,由于某种奇怪的原因,它没有得到 trimmed。就像它的一部分 trimmed 取决于被列入黑名单的单词有多少个字符,但总的来说它没有按预期工作。

它应该做的是将 "replace:" 替换为“”,但这不起作用。

这是我的代码:

@EventHandler public void BlackListWords(AsyncPlayerChatEvent e) { 字符串标签 = "replace:";

for (String s : p.file.getFile().getStringList(p.file.path + ".BlacklistedWords")) {
    String[] parts = labels.split(" ");

        String replace = parts[0];


        Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Words:\n" + s.replace("replace:", ""));
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "StringList:\n" + s.split(" ")[0]);

        if (e.getMessage().equalsIgnoreCase(s.split(" ")[0])) {
            e.setMessage(s.substring(replace.length()).replaceAll("//replace", "").split(" ")[0].replace("_", " "));
    }
}


}

这是一个可能有用的示例。阅读您的代码,您的文件中的字符串列表似乎包含以 "replace: " 作为前缀的行,然后您正在做一些多余的事情来尝试删除该前缀,然后最终使用错误的字符串作为您的信息。看看这是否有助于澄清,但它是基于我假设你正在尝试做的......

import java.util.ArrayList;
import org.junit.Test;

public class Whosebug_32878663 {

    @Test
    public void replaceBlacklistedWords()
    {

        // mimicking your list of strings from the file
        ArrayList<String> wordList = new ArrayList<String>();
        wordList.add("replace: blue");
        wordList.add("replace: green");
        wordList.add("replace: red");
        wordList.add("replace: white");
        wordList.add("replace: yellow");

        String incomingMessage = "I am RED, white, bLuE, and green all over.  What am I?";
        String modifiedMessage = incomingMessage;

        for (String s : wordList)
        {
            // one way of many to remove "replace:" if that is important
            String justTheWord = s.split(" ", 2)[1];  

            // the (?i) tells the regex to perform in a case insensitive manner
            modifiedMessage = modifiedMessage.replaceAll("(?i)" + justTheWord, " ");  
        }

        System.out.println("Original message: " + incomingMessage);
        System.out.println("Modified message: " + modifiedMessage);
    }

}