我想通过分隔符“-”将字符串“004-034556”拆分为两个字符串:
part1 = "004";
part2 = "034556";
这意味着第一个字符串将包含“-”之前的字符,第二个字符串将包括“-”之后的字符。
我还想检查字符串中是否有“-”。
我想通过分隔符“-”将字符串“004-034556”拆分为两个字符串:
part1 = "004";
part2 = "034556";
这意味着第一个字符串将包含“-”之前的字符,第二个字符串将包括“-”之后的字符。
我还想检查字符串中是否有“-”。
当前回答
根据文档:
public String[]split(Stringregex,int limit)围绕给定正则表达式的匹配项拆分此字符串。此方法返回的数组包含此字符串的子字符串,该子字符串由另一个与给定表达式匹配或以一串数组中的子字符串的顺序如下出现在此字符串中。如果表达式与输入,则结果数组只有一个元素,即一串
基本上你可以这样做:
String s = "123-456-789-123"; // The String to be split
String[] array = s.split("-"); // Split according to the hyphen and put them in an array
for(String subString : array){ // Cycle through the array
System.out.println(subString);
}
输出:
123
456
789
123
其他回答
根据文档:
public String[]split(Stringregex,int limit)围绕给定正则表达式的匹配项拆分此字符串。此方法返回的数组包含此字符串的子字符串,该子字符串由另一个与给定表达式匹配或以一串数组中的子字符串的顺序如下出现在此字符串中。如果表达式与输入,则结果数组只有一个元素,即一串
基本上你可以这样做:
String s = "123-456-789-123"; // The String to be split
String[] array = s.split("-"); // Split according to the hyphen and put them in an array
for(String subString : array){ // Cycle through the array
System.out.println(subString);
}
输出:
123
456
789
123
请不要使用StringTokenizer类,因为它是出于兼容性原因而保留的遗留类,并且不鼓励在新代码中使用它。我们也可以使用其他人建议的拆分方法。
String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));
正如预期的那样,它将打印:
[004, 034556]
在这个答案中,我还想指出Java8中拆分方法的一个变化。String#split()方法使用Pattern.split,现在它将删除结果数组开头的空字符串。请注意Java 8文档中的以下更改:
当输入开始时宽度匹配为正时序列,则在开头包含一个空前导子字符串生成的阵列。然而,开头的宽度为零永远不会产生这样的空前导子字符串。
对于以下示例,这意味着:
String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));
我们将得到三个字符串:[0,0,4],而不是像Java7和以前的情况那样得到四个字符串。还要检查这个类似的问题。
这些要求为解释留下了空间。我建议写一个方法,
public final static String[] mySplit(final String s)
其封装了该功能。当然,您可以使用String.split(..),如实现的其他答案中所述。
您应该为输入字符串以及期望的结果和行为编写一些单元测试。
优秀的考生应包括:
- "0022-3333"
- "-"
- "5555-"
- "-333"
- "3344-"
- "--"
- ""
- "553535"
- "333-333-33"
- "222--222"
- "222--"
- "--4555"
通过定义相应的测试结果,您可以指定行为。
例如,如果“-333”应在[,333]中返回,或者如果它是一个错误。“333-333-33”是否可以在[333333-33]或[3333-333,33]中分开,或者这是一个错误?等等
我查看了所有答案,发现所有答案都是第三方许可或基于正则表达式的。
下面是我使用的一个很好的哑实现:
/**
* Separates a string into pieces using
* case-sensitive-non-regex-char-separators.
* <p>
* <code>separate("12-34", '-') = "12", "34"</code><br>
* <code>separate("a-b-", '-') = "a", "b", ""</code>
* <p>
* When the separator is the first character in the string, the first result is
* an empty string. When the separator is the last character in the string the
* last element will be an empty string. One separator after another in the
* string will create an empty.
* <p>
* If no separators are set the source is returned.
* <p>
* This method is very fast, but it does not focus on memory-efficiency. The memory
* consumption is approximately double the size of the string. This method is
* thread-safe but not synchronized.
*
* @param source The string to split, never <code>null</code>.
* @param separator The character to use as splitting.
* @return The mutable array of pieces.
* @throws NullPointerException When the source or separators are <code>null</code>.
*/
public final static String[] separate(String source, char... separator) throws NullPointerException {
String[] resultArray = {};
boolean multiSeparators = separator.length > 1;
if (!multiSeparators) {
if (separator.length == 0) {
return new String[] { source };
}
}
int charIndex = source.length();
int lastSeparator = source.length();
while (charIndex-- > -1) {
if (charIndex < 0 || (multiSeparators ? Arrays.binarySearch(separator, source.charAt(charIndex)) >= 0 : source.charAt(charIndex) == separator[0])) {
String piece = source.substring(charIndex + 1, lastSeparator);
lastSeparator = charIndex;
String[] tmp = new String[resultArray.length + 1];
System.arraycopy(resultArray, 0, tmp, 1, resultArray.length);
tmp[0] = piece;
resultArray = tmp;
}
}
return resultArray;
}
使用流拆分并打印字符串
String input = "004-034556";
Stream<String> stream = Arrays.stream(input.split( "-" ));
stream.forEach(System.out::println);