如何在Java中使用split()字符串方法分隔单词

时间:2018-08-21 10:01:47

标签: java arrays string split

我想分离单词并在一行之间加上连字符(-)将它们打印出来。我已经编写了以下代码,但它只显示最后一个单词,后跟一个连字符,即输出为胡萝卜-。我不明白为什么以及要进行哪些更改才能获得所需的输出?

public class SeparatingWords {

    public static void main(String[] args) {
        String str = "apple banana carrot";
        System.out.println(separatingWords(str));
    }

    public static String separatingWords(String str) {
        String[] words = str.split(" ");
        String result = null;

        for (int i = 0; i < words.length; i++) {
            result=words[i]+"-";
        }

        return result;
    }
}

5 个答案:

答案 0 :(得分:4)

为什么不直接调用split并串联字符串,为什么不能直接直接调用replaceAll来实现目标。这将使您的代码变得简单。

String result = str.replaceAll(" ", "-");

下面是您的示例修改代码。希望这会有所帮助

public class Sample {

public static void main(String[] args) {
    String str = "apple banana carrot";
    System.out.println(separatingWords(str));
 }

public static String separatingWords(String str) {
    String result = str.replaceAll(" ", "-");
    return result;
 }
}

如果您要根据方法内的要求执行任何其他操作,则下面的方法适用于您。如@Moler所建议,添加了+=并初始化了result对象

public static String separatingWords(String str) {
    String[] words = str.split(" ");
    String result = "";  // Defaulted the result

    for (int i = 0; i < words.length-1; i++) {
        result += words[i] + "-";  // Added a +=
    }
    result += words[words.length - 1];
    return result;
}

答案 1 :(得分:1)

   public class SeparatingWords
    {
        public static void main(String[] args)
        {
            String str="apple banana carrot";
            System.out.println(separatingWords(str));
        }
        public static String separatingWords(String str)
        {
            String[] words=str.split(" ");
            String result="";

            for(int i=0;i<words.length;i++)
            {
                result += words[i]+"-";
            }
            return result;
        }
    }

答案 2 :(得分:0)

尝试以下代码:

public class SeparatingWords
{
public static void main(String[] args)
{
    String str="apple banana carrot";
    System.out.println(separatingWords(str));
}
public static String separatingWords(String str)
{
    String[] words=str.split(" ");

    String result=words[0];

    for(int i=1;i<words.length;i++)
    {
        result=result+"-"+words[i];
    }
    return result;
}
}

答案 3 :(得分:0)

您可以使用s StringBuilder,在单词后面加上一个连音,在最后一个单词上,只需添加单词:

public class SeparatingWords {

    public static void main(String[] args) {
        String str = "apple banana carrot";
        System.out.println(separatingWords(str));
    }

    public static String separatingWords(String str) {
        String[] words = str.split(" ");
        StringBuilder resultBuilder = new StringBuilder();

        for (int i = 0; i < words.length; i++) {
            resultBuilder.append(words[i]);
            if (i != words.length - 1) {
                resultBuilder.append("-");
            }
        }

        return resultBuilder.toString();
    }
}

答案 4 :(得分:0)

String[] words = str.split(" ");
// perform operations on individual words
return String.join("-", words);