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

时间:2019-06-26 07:48:35

标签: java str-replace

我已经有一段时间了,尝试一些不同的代码。当我尝试替换单词时说替换代码是错误的。如何替换消息中的所有单词?

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);

6 个答案:

答案 0 :(得分:1)

您要先替换,然后再分配值,并在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);

答案 1 :(得分:1)

在此代码中,当您调用replaceAll时,message为null。 您必须在分配后给它打电话。

 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);

答案 2 :(得分:0)

您实际上已将内容分配给邮件后,需要进行替换

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

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

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

答案 3 :(得分:0)

您需要在此处更改两件事

初始化变量

   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);

答案 4 :(得分:0)

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

    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

答案 5 :(得分:0)

您要替换字符串之前,以设置消息。考虑更改顺序:

        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);

请查看以下主题提示:

message = message + "hello";

与以下相同:

message += "hello";