如何替换字符串消息中的单词?

How to replace a word in string message?

我在这方面已经有一段时间尝试了一些不同的代码。当我尝试替换单词时说我的替换代码是错误的。我怎样才能替换消息中的所有单词?

public class StorySynthesiser {

    public static void main(String[] args) {

        String message;
        String name ="Tiger";
        String result = message.replaceAll("Hare", name);


        message = "";
        message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
        message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
        message = message +"All the animals in the forest gathered to watch.\n";
        message = message +"The Hare ran down the road for a while and then paused to rest. \n";
        message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
        message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
        message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
        message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";



        JOptionPane.showMessageDialog(null, message);

您需要在您实际为邮件分配内容后进行替换:

String message = "content here...";
String result = message.replaceAll("Hare", name);

请注意,由于您实际上并没有进行正则表达式替换,因此您不妨在此处使用 String#replace 以获得更好的性能:

String result = message.replace("Hare", name);

您要先替换,然后再赋值,还要在您的 DialogPane 中显示另一个变量,因此您需要这样做:

    String name ="Tiger";


    message = "";
    message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
    message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
    message = message +"All the animals in the forest gathered to watch.\n";
    message = message +"The Hare ran down the road for a while and then paused to rest. \n";
    message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
    message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
    message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
    message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";

   String result = message.replaceAll("Hare", name);

    JOptionPane.showMessageDialog(null, result);

您需要在此处更改两件事

初始化变量

   String message;

并在末尾调用替换,如下所示。

    String message = "";
    String name ="Tiger";


    message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
    message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
    message = message +"All the animals in the forest gathered to watch.\n";
    message = message +"The Hare ran down the road for a while and then paused to rest. \n";
    message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
    message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
    message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
    message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";

    String result = message.replaceAll("Hare", name);


    JOptionPane.showMessageDialog(null, message);

您必须在声明期间初始化您的 message 变量,因为它是所有同步的,您必须在追加 message 变量后放置 replaceAll ,否则您将得到一个空的字符串.

    public class StorySynthesiser  {

    public static void main(String[] args) {

        String message=""; // initailize your message here
        String name ="Tiger";
        message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
        message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
        message = message +"All the animals in the forest gathered to watch.\n";
        message = message +"The Hare ran down the road for a while and then paused to rest. \n";
        message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
        message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
        message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
        message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";
String result = message.replaceAll("Hare", name);
    System.out.println(result);

    }}

工作repl

在此代码中,当您调用replaceAll 时,消息为空。 你必须在分配后调用它。

 String message = "";
String name ="Tiger";


message += "There once was a speedy Hare who bragged about how fast he could run.\n";
message += "Tired of hearing him boast, the Tortoise challenged him to a race.\n";
message += "All the animals in the forest gathered to watch.\n";
message += "The Hare ran down the road for a while and then paused to rest. \n";
message += "He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
message += "The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
message += "The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
message += "Tortoise had already crossed the finish line in 2 hours ago/n";


String result = message.replaceAll("Hare", name);
JOptionPane.showMessageDialog(null, result);

您正在 替换字符串,然后 设置消息。考虑更改顺序:

        String message; //message is now null, trying to replace will cause a Null pointer exception.
        String name ="Tiger";


        message = ""; //message is now an empty string, replaceing something will be useless as there is nothing there.
        message = message +"There once was a speedy Hare who bragged about how fast he could run.\n";
        message = message +"Tired of hearing him boast, the Tortoise challenged him to a race.\n";
        message = message +"All the animals in the forest gathered to watch.\n";
        message = message +"The Hare ran down the road for a while and then paused to rest. \n";
        message = message +"He looked back at the tortoise and cried out, \"How do you expect to win this race when you are walking along at your slow, slow pace?\n";
        message = message +"The Tortoise walked and walked, never ever stopping until he came to the finish line.\n";       
        message = message +"The animals who were watching cheered so loudly for Tortoise that they woke up the Hare. The Hare stretched, yawned and began to run again, but it was too late.\n";
        message = message +"Tortoise had already crossed the finish line in 2 hours ago/n";


        //here message is full and has some "Hare" to replace.
        String result = message.replaceAll("Hare", name);


        JOptionPane.showMessageDialog(null, message);

检查此 off-topic 提示:

message = message + "hello";

等同于:

message += "hello";