英文:
Splitting at every n-th separator, and keeping the character
问题
需要一个函数来将一个字符串更改为如下格式的字符串数组:
Red, Green,
Blue, Orange,
Pink, Gray,
Purple
在这个示例中,分隔符是“,”并且每隔2个元素进行拆分。
有许多类似的在线函数可以实现这个功能,但它们都会删除分隔符。
英文:
I need a function to change a String like this:
Red, Green, Blue, Orange, Pink, Gray, Purple
Into a String[] like this:
Red, Green,
Blue, Orange,
Pink, Gray,
Purple
In this example the character is ,
and splitting it every 2nd.
There are many similar functions online to this one, but they all remove the character.
答案1
得分: 1
这应该可以正常工作。
String sample = "红色,绿色,蓝色,橙色,粉色,灰色,紫色";
ArrayList<String> output = new ArrayList<>();
Pattern firstPattern = Pattern.compile("[\\u4e00-\\u9fa5,\\s]*,[\\u4e00-\\u9fa5,\\s]*|");
Matcher firstMatcher = firstPattern.matcher(sample);
Pattern secondPattern = Pattern.compile("[\\u4e00-\\u9fa5,\\s]*$");
Matcher secondMatcher = secondPattern.matcher(sample);
while (firstMatcher.find()) {
output.add(firstMatcher.group());
}
if ((output.size() * 2) < sample.split(",").length)
if (secondMatcher.find())
output.add(secondMatcher.group(0));
output:
红色, 绿色,
蓝色, 橙色,
粉色, 灰色,
紫色
英文:
this should work fine.
String sample = "Red, Green, Blue, Orange, Pink, Gray, Purple";
ArrayList<String> output = new ArrayList<>();
Pattern firstPattern = Pattern.compile("[a-zA-Z-\\s]*,[a-zA-Z-\\s]*,|[a-zA-Z-\\s]*,[a-zA-Z-\\s]*");
Matcher firstMatcher = firstPattern.matcher(sample);
Pattern secondPattern = Pattern.compile("[a-zA-Z-\\s]*$");
Matcher secondMatcher = secondPattern.matcher(sample);
while (firstMatcher.find()) {
output.add(firstMatcher.group());
}
if ((output.size() * 2) < sample.split(",").length)
if (secondMatcher.find())
output.add(secondMatcher.group(0));
output:
Red, Green,
Blue, Orange,
Pink, Gray,
Purple
答案2
得分: 1
这里有一个在线性时间复杂度 O(n) 内运行的简单解决方案:
where groupSize = 2
public static ArrayList<String> splitPairs(String input, int groupSize) {
String[] inputArray = input.split(",");
ArrayList<String> result = new ArrayList<>();
int index = 0;
while (index < inputArray.length) {
StringBuilder newItem = new StringBuilder();
for (int i = 0; i < groupSize && index < inputArray.length; i++, index++) {
if (i != 0)
newItem.append(", ");
newItem.append(inputArray[index].trim());
if (i == groupSize - 1) {
newItem.append(",");
}
}
if (newItem.length() > 0) {
result.add(newItem.toString());
}
}
return result;
}
英文:
here is a simple solution that works in linear time O(n)
where groupSize = 2
public static ArrayList<String> splitPairs(String input, int groupSize) {
String[] inputArray = input.split(",");
ArrayList<String> result = new ArrayList<>();
int index = 0;
while (index < inputArray.length) {
StringBuilder newItem = new StringBuilder();
for (int i = 0; i < groupSize && index < inputArray.length; i++, index++) {
if (i != 0)
newItem.append(", ");
newItem.append(inputArray[index].trim());
if (i == groupSize - 1) {
newItem.append(",");
}
}
if (newItem.length() > 0) {
result.add(newItem.toString());
}
}
return result;
}
答案3
得分: 0
A not very good solution may help you.
First: 将字符串按','拆分为String[]
Second: 将String数组拆分为字符串数组的数组,每个小数组包含0、1或2个元素
Third: 合并小字符串数组。
使用Google Guava的解决方案:
String input = "Red, Green, Blue, Orange, Pink, Gray, Purple";
List<List<String>> lists = Lists.partition(Arrays.asList(input.split(",")), 2);
String[] outputs = new String[lists.size()];
lists.stream().map(strings -> String.join(", ", strings)).map(String::trim)
.collect(Collectors.toList()).toArray(outputs);
for (String output: outputs) {
System.out.println(output);
}
英文:
A not very good solution may help you.
First:split the string by ',' into String[]
Second:split String array into array of string array and every small array have 0,1 or 2 elements
Third:join the small string array.
A solution use google guava:
String input = "Red, Green, Blue, Orange, Pink, Gray, Purple";
List<List<String>> lists = Lists.partition(Arrays.asList(input.split(",")),2);
String[] outputs = new String[lists.size()];
lists.stream().map(strings -> String.join(", ", strings)).map(String::trim)
.collect(Collectors.toList()).toArray(outputs);
for (String output: outputs) {
System.out.println(output);
}
答案4
得分: 0
以下是没有使用正则表达式的方法,使用了我在评论中提到的indexOf
方法。这种方法有点老式,完全手动处理所有操作,基本上只使用了indexOf
和substring
,以满足您的“保留字符”的要求。
public static void main(String[] args) {
String input = "Red, Green, Blue, Orange, Pink, Gray, Purple";
final int n = 2;
final char delim = ',';
nthSep(input, n, delim).stream().forEach(System.out::println);
}
private static LinkedList<String> nthSep(String input, int n, char delim) {
LinkedList<String> res = new LinkedList<>();
int startsplit = 0;
int delimcount = 0;
int curInd = 0;
//repeat until no separator is found
while (true) {
//remember the index of the current split part
startsplit = curInd;
delimcount = 0;
//find the separator n times
while (delimcount < n) {
curInd = input.indexOf(delim, curInd+1);
if (curInd == -1) {
break;
}
delimcount++;
}
if(curInd != -1){
//add a result to the list, then move on with the next split part
res.add(input.substring(startsplit, curInd+1));
curInd++;
} else {
//so further separator is found, add the whole remaining input
res.add(input.substring(startsplit));
break;
}
}
return res;
}
希望对您有所帮助。
英文:
Here is an approach withouth regex, using the indexOf
method that i mentioned in the comment. It's a little oldschool and does all the things manually, essentially just using indexOf
and substring
, which makes it fulfill your "keeping the character" requirement.
public static void main(String[] args) {
String input = "Red, Green, Blue, Orange, Pink, Gray, Purple";
final int n = 2;
final char delim = ',';
nthSep(input, n, delim).stream().forEach(System.out::println);
}
private static LinkedList<String> nthSep(String input, int n, char delim) {
LinkedList<String> res = new LinkedList<>();
int startsplit = 0;
int delimcount = 0;
int curInd = 0;
//repeat until no separator is found
while (true) {
//remember the index of the current split part
startsplit = curInd;
delimcount = 0;
//find the separator n times
while (delimcount < n) {
curInd = input.indexOf(delim, curInd+1);
if (curInd == -1) {
break;
}
delimcount++;
}
if(curInd != -1){
//add a result to the list, then move on with the next split part
res.add(input.substring(startsplit, curInd+1));
curInd++;
} else {
//so further separator is found, add the whole remaining input
res.add(input.substring(startsplit));
break;
}
}
return res;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论