有一个字符串,它是一个分隔的字符串:item_1|item_2|item_3
,在此示例中,分隔符号是|
。
我的老板不喜欢split
方法来获取字符串的不同部分:他认为存在这种风险,但是他不确定这是什么风险。那么,还有其他方法可以从单独的String中获取List
吗?
答案 0 :(得分:4)
import java.util.ArrayList;
import java.util.List;
public class SplitUsingAnotherMethodBecauseBossLikesWastingEveryonesTime {
public static void main(String[] args) {
System.out.println(split("Why would anyone want to write their own String split function in Java?", ' '));
System.out.println(split("The|Split|Method|Is|Way|More|Flexible||", '|'));
}
private static List<String> split(String input, char delimiter) {
List<String> result = new ArrayList<>();
int idx = 0;
int next;
do {
next = input.indexOf(delimiter, idx);
if (next > -1) {
result.add(input.substring(idx, next));
idx = next + 1;
}
} while(next > -1);
result.add(input.substring(idx));
return result;
}
}
输出...
[Why, would, anyone, want, to, write, their, own, String, split, function, in, Java?]
[The, Split, Method, Is, Way, More, Flexible, , ]
答案 1 :(得分:1)
您可以遍历字符串中的所有char
,然后使用substring()
选择不同的子字符串:
public static List<String> split(String input, char delimiter) {
List<String> output = new LinkedList<>();
int lastIndex = 0;
boolean doubleQuote = false;
boolean singleQuoteFound = false;
for (int i = 0, current, last = 0, length = input.length(); i < length; i++) {
current = input.charAt(i);
if (last != '\\') {
if (current == '"') {
doubleQuote = !doubleQuote;
} else if (current == '\'') {
singleQuoteFound = !singleQuoteFound;
} else if (current == delimiter && !doubleQuote && !singleQuoteFound) {
output.add(input.substring(lastIndex, i));
lastIndex = i + 1;
}
}
last = current;
}
output.add(input.substring(lastIndex));
return output;
}
这是一种非常粗糙的方法,但是从我的测试来看,它应该处理转义的分隔符,单引号'
和/或双引号"
中的分隔符。
可以这样称呼:
List<String> splitted = split("Hello|World|"No|split|here"|\|Was escaped|'Some|test'", '|');
打印:
[Hello, World, "No|split|here", \|Was escaped, 'Some|test']
答案 2 :(得分:-1)
当我们使用分割字符串时,它会在内部创建Patterns对象,该对象的开销很大,但这仅适用于Java 7之前的版本,在Java 7/8中,它使用的索引为 由于Java 7不会有任何正则表达式引擎的开销,但是,如果您传递更复杂的表达式,它将恢复为编译新模式,此处的行为应与Java 6相同 您可以使用预编译模式并拆分字符串。
public class MyClass {
static Pattern pattern = Pattern.compile("\\|");
public static void main(String[] args) {
String str = "item_1|item_2|item_3";
Stream<String> streamsName = pattern.splitAsStream(str);
streamsName.forEach(System.out::println);
}
}