[英]Splitting at every n-th separator, and keeping the character
我需要一個 function 來更改這樣的字符串:
Red, Green, Blue, Orange, Pink, Gray, Purple
像這樣進入 String[] :
Red, Green,
Blue, Orange,
Pink, Gray,
Purple
在此示例中,字符為,
並且每隔 2 次拆分一次。 網上有很多和這個類似的功能,但是都去掉了字符。
這應該可以正常工作。
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
這是一個在線性時間 O(n) 內工作的簡單解決方案
其中組大小 = 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;
}
一個不太好的解決方案可能會對您有所幫助。
首先:將字符串按','分割成String[]
第二:將字符串數組拆分為字符串數組,每個小數組有0,1或2個元素
第三:加入小字符串數組。
使用谷歌番石榴的解決方案:
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);
}
這是一種沒有正則表達式的方法,使用我在評論中提到的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;
}
嘗試這個。
Pattern TWO_WORDS = Pattern.compile("[^,\\s]+(,\\s*[^,\\s]+)?(,\\s*)?");
String input = "Red, Green, Blue, Orange, Pink, Gray, Purple";
String[] result = TWO_WORDS.matcher(input).results()
.map(m -> m.group())
.toArray(String[]::new);
for (String s : result)
System.out.println("#" + s + "#");
output
#Red, Green, #
#Blue, Orange, #
#Pink, Gray, #
#Purple#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.