如何从字符串中提取单词?

时间:2014-04-06 20:31:24

标签: java string joptionpane

我做了一些研究,我只能找到搜索结果的答案。我试图从字符串中提取一个单词并从另一个字符串中添加一个单词,替换已删除的单词。每种情况都不同,所以我不能把它变成一个常数。我非常感谢你的帮助!

package rudolph;

import javax.swing.JOptionPane;

public class IfBlankWasBlank {
    public static void main(String[] args) {
        String charInput = "";
        String thing = "";
        String pun = "";

        charInput = JOptionPane.showInputDialog(null,
                "Enter the person or thing you'd like to make fun of:");
        thing = JOptionPane.showInputDialog(null,
                "Enter the thing that " + charInput +" is doing:");
        pun = JOptionPane.showInputDialog(null,
                "Enter the pun that " + charInput + " is doing:");

        String msg = "If " + charInput + " was " + thing + ", then they'd be " + pun + ".";


        JOptionPane.showMessageDialog(null, msg);
    }
}

因此,如果我在CharInput字符串中输入红鼻子驯鹿的Rudolph,thing字符串中的节能和pun字符串中的LED,我想要{{1如果“红鼻子驯鹿鲁道夫节能,那么他们就是鲁道夫的LED鼻子驯鹿。” 我知道这很傻,但我想知道如何利用它如果可以的话!非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

你应该使用字符串拆分方法。String#split()