[英]How do I separate words using split() string method in Java
I want to separate words and print them in a single line with a hyphen(-) in between. 我想分开单词,并在它们之间用连字符(-)将它们打印在一行中。 I have written the following code but it only prints the last word followed by a hyphen ie the output is carrot-.
我已经编写了以下代码,但它只显示最后一个单词,后跟一个连字符,即输出为胡萝卜。 I don't understand why and what changes do I make to get the desired output?
我不明白为什么以及要进行哪些更改才能获得所需的输出?
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;
}
}
Instead of calling a split
and concatenating the string, why can't you directly call replaceAll
directly to achieve your goal. 为什么不直接调用
replaceAll
来实现目标,而不是调用split
和串联字符串。 This will make your code simple. 这将使您的代码变得简单。
String result = str.replaceAll(" ", "-");
Below is sample modified code of yours. 以下是您的示例修改代码。 Hope this helps
希望这可以帮助
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;
}
}
If you want to perform any other operation based on your requirement inside the method, then below should work for you. 如果您要根据方法中的要求执行任何其他操作,则以下方法将为您工作。 As suggested by @Moler added
+=
and initialized the result
object 如@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;
}
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;
}
}
Try this code: 试试这个代码:
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;
}
}
You could use s StringBuilder
, append the single word and a hyphon and at the last word, you just append the word: 您可以使用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();
}
}
String[] words = str.split(" ");
// perform operations on individual words
return String.join("-", words);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.