猿问

除了拆分之外,还有其他方法可以从字符串中获取列表吗?

有一个字符串是一个分隔字符串:item_1|item_2|item_3,在这个例子中分隔符号是|

我的老板不喜欢split获取字符串不同部分的方法:他认为这样做有风险,但他不太确定风险是什么。那么还有其他方法可以List从单独的字符串中获取 a 吗?


慕妹3146593
浏览 166回答 3
3回答

温温酱

import java.util.ArrayList;import java.util.List;public class SplitUsingAnotherMethodBecauseBossLikesWastingEveryonesTime {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(split("Why would anyone want to write their own String split function in Java?", ' '));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(split("The|Split|Method|Is|Way|More|Flexible||", '|'));&nbsp; &nbsp; }&nbsp; &nbsp; private static List<String> split(String input, char delimiter) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> result = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int idx = 0;&nbsp; &nbsp; &nbsp; &nbsp; int next;&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next = input.indexOf(delimiter, idx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(input.substring(idx, next));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idx = next + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while(next > -1);&nbsp; &nbsp; &nbsp; &nbsp; result.add(input.substring(idx));&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}输出...[Why, would, anyone, want, to, write, their, own, String, split, function, in, Java?][The, Split, Method, Is, Way, More, Flexible, , ]

守着星空守着你

您可以只遍历char字符串中的所有 s,然后用于substring()选择不同的子字符串:public static List<String> split(String input, char delimiter) {&nbsp; &nbsp; List<String> output = new LinkedList<>();&nbsp; &nbsp; int lastIndex = 0;&nbsp; &nbsp; boolean doubleQuote = false;&nbsp; &nbsp; boolean singleQuoteFound = false;&nbsp; &nbsp; for (int i = 0, current, last = 0, length = input.length(); i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; current = input.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if (last != '\\') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (current == '"') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doubleQuote = !doubleQuote;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (current == '\'') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; singleQuoteFound = !singleQuoteFound;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (current == delimiter && !doubleQuote && !singleQuoteFound) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.add(input.substring(lastIndex, i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastIndex = i + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; last = current;&nbsp; &nbsp; }&nbsp; &nbsp; output.add(input.substring(lastIndex));&nbsp; &nbsp; return output;}这是一种非常粗略的方法,但从我的测试来看,它应该处理转义分隔符、单引号'和/或双"引号中的分隔符。可以这样调用:List<String> splitted = split("Hello|World|"No|split|here"|\|Was escaped|'Some|test'", '|');印刷:[Hello, World, "No|split|here", \|Was escaped, 'Some|test']

开满天机

当我们使用拆分字符串时,它会在内部创建 Patterns 对象,该对象会产生开销,但这仅适用于 Java 7 之前的版本,在 Java 7/8 中,它使用自 java 7 以来的索引,它不会有任何正则表达式引擎的开销。但是,如果您确实传递了一个更复杂的表达式,它会恢复为编译一个新模式,这里的行为应该与 Java 6 上的行为相同,您可以使用预编译模式并拆分字符串。public class MyClass {static Pattern pattern = Pattern.compile("\\|");public static void main(String[] args) {&nbsp; &nbsp; String str = "item_1|item_2|item_3";&nbsp; &nbsp; Stream<String> streamsName = pattern.splitAsStream(str);&nbsp; &nbsp; streamsName.forEach(System.out::println);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答