【发布时间】:2019-11-17 22:31:18
【问题描述】:
我正在尝试将字符串拆分为数组。这应该没问题,因为str.split(" ") 应该可以正常工作,但是字符串实际上是"xyz 100b\nabc 200b\ndef 400b" 的形式。我想知道处理这个问题的最佳方法是什么。我还需要以他们提供的格式返回 4 个字符串。下面是我现在尝试的方法,但它没有正确拆分数组。我的目标是将数组拆分为["xyz", "100b", "abc", "200b", "def", "400b"]
public static String solution(String words){
String array[] = words.split(" ");
/*
There's a lot of other code manipulating the array to get 4 figures in the
end. This doesn't matter, it's just splitting the array and the return that
is my issue
In the end I will have 4 integers that I want to return in a similar way
that they gave them to me.
*/
return "answer 1" + String.valueOf(num1) + "b\n" +
"answer2 " + String.valueOf(num2) + "b\n" +
"answer 3" + String.valueOf(num3) + "b\n" +
"answer4 " + String.valueOf(num4) + "b\n";
}
编辑:
String array [] = str.split("\n| ") 将根据需要拆分数组,谢谢 A.Oubidar
【问题讨论】:
-
你能用你目前尝试过的代码更新你的问题吗?
-
对不起,我解释得很糟糕,这只是我试图寻求帮助的拆分和返回。 A.Oubidar 实际上给了我足够的答案
标签: java