我想通过分隔符“-”将字符串“004-034556”拆分为两个字符串:
part1 = "004";
part2 = "034556";
这意味着第一个字符串将包含“-”之前的字符,第二个字符串将包括“-”之后的字符。
我还想检查字符串中是否有“-”。
我想通过分隔符“-”将字符串“004-034556”拆分为两个字符串:
part1 = "004";
part2 = "034556";
这意味着第一个字符串将包含“-”之前的字符,第二个字符串将包括“-”之后的字符。
我还想检查字符串中是否有“-”。
当前回答
String s = "TnGeneral|DOMESTIC";
String a[]=s.split("\\|");
System.out.println(a.toString());
System.out.println(a[0]);
System.out.println(a[1]);
输出:
TnGeneral
DOMESTIC
其他回答
我查看了所有答案,发现所有答案都是第三方许可或基于正则表达式的。
下面是我使用的一个很好的哑实现:
/**
* 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.split作为一个字符分隔符,否则您不关心性能
如果性能不是问题,或者分隔符是不是正则表达式特殊字符的单个字符(即,不是.$|()[{^?*+\之一),则可以使用String.split。
String[] results = input.split(",");
如果delimeter是单个字符而不在上面的列表中,则split方法有一个优化,以避免使用正则表达式。否则,它必须编译正则表达式,这是不理想的。
如果使用复杂的分隔符并且您关心性能,请使用Pattern.split并预编译模式。
如果性能是一个问题,并且分隔符不是上述问题之一,那么应该预先编译一个正则表达式模式,然后可以重用它。
// Save this somewhere
Pattern pattern = Pattern.compile("[,;:]");
/// ... later
String[] results = pattern.split(input);
最后一个选项仍然会创建一个新的Matcher对象。您还可以缓存此对象并为每个输入重置它以获得最大性能,但这有点复杂,而且不线程安全。
你也可以这样做
String concatenated_String="hi^Hello";
String split_string_array[]=concatenated_String.split("\\^");
这些要求为解释留下了空间。我建议写一个方法,
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]中分开,或者这是一个错误?等等
对于简单的用例,String.split()应该完成这项工作。如果您使用番石榴,还有一个Splitter类,它允许链接不同的字符串操作并支持CharMatcher:
Splitter.on('-')
.trimResults()
.omitEmptyStrings()
.split(string);