I have a string, "004-034556"
, that I want to split into two strings:
我有一个字符串,“004-034556”,我想把它分成两个字符串:
string1=004
string2=034556
That means the first string will contain the characters before '-'
, and the second string will contain the characters after '-'
. I also want to check if the string has '-'
in it. If not, I will throw an exception. How can I do this?
这意味着第一个字符串将包含“-”之前的字符,第二个字符串将包含“-”之后的字符。我还想检查字符串是否有“-”。如果没有,我将抛出一个异常。我该怎么做呢?
31 个解决方案
#1
2230
Just use the appropriate method: String#split()
.
只需使用适当的方法:String#split()。
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Note that this takes a regular expression, so remember to escape special characters if necessary.
注意,这需要一个正则表达式,所以如果需要,记得要避开特殊字符。
there are 12 characters with special meanings: the backslash
\
, the caret^
, the dollar sign$
, the period or dot.
, the vertical bar or pipe symbol|
, the question mark?
, the asterisk or star*
, the plus sign+
, the opening parenthesis(
, the closing parenthesis)
, and the opening square bracket[
, the opening curly brace{
, These special characters are often called "metacharacters".有12个字符的特殊含义:反斜杠\,插入符号^,美元符号$,周期或点,竖线或管道符号|,问号?,星号或* *,加号+,括号(关闭括号)开幕式,开幕式方括号[,左括号{,这些特殊字符通常被称为“元字符”。
So, if you want to split on e.g. period/dot .
which means "any character" in regex, use either backslash \
to escape the individual special character like so split("\\.")
, or use character class []
to represent literal character(s) like so split("[.]")
, or use Pattern#quote()
to escape the entire string like so split(Pattern.quote("."))
.
所以,如果你想在周期/点上分开。在regex中,它的意思是“任何字符”,使用反斜杠\以避免单独的特殊字符,如so split(“\\.”),或者使用字符类[]来表示文字字符,例如so split(“[.]”),或者使用Pattern#quote()来转义整个字符串,如so split(Pattern.quote(“。”))。
String[] parts = string.split(Pattern.quote(".")); // Split on period.
To test beforehand if the string contains certain character(s), just use String#contains()
.
要预先测试字符串是否包含某些字符,只需使用string # include()。
if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Note, this does not take a regular expression. For that, use String#matches()
instead.
注意,这不是一个正则表达式。为此,使用字符串#匹配()代替。
If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<=
group on the pattern.
如果你想保留在结果部分中的分割字符,然后使用积极的lookaround。如果你想让分裂的字符以左手边结束,请在模式上使用前缀?<=组。
String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556
In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?=
group on the pattern.
如果你想让这个分裂的字符在右边结束,那么在模式上使用前面的前缀?=组。
String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556
If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split()
method.
如果您想要限制产生的部分的数量,那么您可以提供所需的编号作为split()方法的第二个参数。
String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42
#2
65
An alternative to processing the string directly would be to use a regular expression with capturing groups. This has the advantage that it makes it straightforward to imply more sophisticated constraints on the input. For example, the following splits the string into two parts, and ensures that both consist only of digits:
直接处理字符串的另一种方法是使用带有捕获组的正则表达式。这样做的好处是,它可以直接地表示对输入的更复杂的约束。例如,下面将字符串拆分为两个部分,并确保它们都只包含数字:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class SplitExample
{
private static Pattern twopart = Pattern.compile("(\\d+)-(\\d+)");
public static void checkString(String s)
{
Matcher m = twopart.matcher(s);
if (m.matches()) {
System.out.println(s + " matches; first part is " + m.group(1) +
", second part is " + m.group(2) + ".");
} else {
System.out.println(s + " does not match.");
}
}
public static void main(String[] args) {
checkString("123-4567");
checkString("foo-bar");
checkString("123-");
checkString("-4567");
checkString("123-4567-890");
}
}
As the pattern is fixed in this instance, it can be compiled in advance and stored as a static member (initialised at class load time in the example). The regular expression is:
由于该模式在此实例中是固定的,因此可以预先编译并存储为静态成员(在示例中初始化在类加载时)。正则表达式是:
(\d+)-(\d+)
The parentheses denote the capturing groups; the string that matched that part of the regexp can be accessed by the Match.group() method, as shown. The \d matches and single decimal digit, and the + means "match one or more of the previous expression). The - has no special meaning, so just matches that character in the input. Note that you need to double-escape the backslashes when writing this as a Java string. Some other examples:
括号表示捕获组;与regexp部分匹配的字符串可以通过Match.group()方法访问,如所示。\d匹配和一个十进制数字,而+的意思是“匹配一个或多个前面的表达式)。没有特殊的含义,所以只匹配输入中的字符。注意,当将其作为Java字符串写入时,您需要双击反斜杠。一些其他的例子:
([A-Z]+)-([A-Z]+) // Each part consists of only capital letters
([^-]+)-([^-]+) // Each part consists of characters other than -
([A-Z]{2})-(\d+) // The first part is exactly two capital letters,
// the second consists of digits
#3
35
String[] result = yourString.split("-");
if (result.length != 2)
throw new IllegalArgumentException("String not in correct format");
This will split your string into 2 parts. The first element in the array will be the part containing the stuff before the -
, and the 2nd element in the array will contain the part of your string after the -
.
这将把你的字符串分成两部分。数组中的第一个元素将是包含前面内容的部分,数组中的第二个元素将包含字符串后面的部分。
If the array length is not 2, then the string was not in the format: string-string
.
如果数组长度不是2,那么字符串不是格式:字符串。
Check out the split()
method in the String
class.
检查String类中的split()方法。
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html # split-java.lang.String-int -
#4
25
// This leaves the regexes issue out of question
// But we must remember that each character in the Delimiter String is treated
// like a single delimiter
public static String[] SplitUsingTokenizer(String subject, String delimiters) {
StringTokenizer strTkn = new StringTokenizer(subject, delimiters);
ArrayList<String> arrLis = new ArrayList<String>(subject.length());
while(strTkn.hasMoreTokens())
arrLis.add(strTkn.nextToken());
return arrLis.toArray(new String[0]);
}
#5
21
String[] out = string.split("-");
should do thing you want. String class has many method to operate with string.
应该做你想做的事。String类有许多使用字符串操作的方法。
#6
16
The requirements left room for interpretation. I recommend writing a method,
这些要求留下了解释的余地。我建议写一种方法,
public final static String[] mySplit(final String s)
which encapsulate this function. Of course you can use String.split(..) as mentioned in the other answers for the implementation.
这个函数封装。当然,您可以使用String.split(..)作为实现的其他答案。
You should write some unit-tests for input strings and the desired results and behaviour.
您应该为输入字符串和期望的结果和行为编写一些单元测试。
Good test candidates should include:
优秀的应试者应该包括:
- "0022-3333"
- "-"
- "5555-"
- "-333"
- "3344-"
- "--"
- ""
- "553535"
- "333-333-33"
- "222--222"
- "222--"
- "--4555"
With defining the according test results, you can specify the behaviour.
通过定义测试结果,您可以指定行为。
For example, if "-333"
should return in [,333]
or if it is an error. Can "333-333-33"
be separated in [333,333-33] or [333-333,33]
or is it an error? And so on.
例如,如果“-333”应该返回[333],或者它是一个错误。“333- 3333 -33”可以在[3333 -33]或[333-333,33]中分开吗?还是一个错误?等等。
#7
14
Assuming, that
假设,
- you don't really need regular expressions for your split
- 你并不是真的需要正则表达式来进行拆分。
- you happen to already use apache commons lang in your app
- 你已经在app中使用了apache commons lang。
The easiest way is to use StringUtils#split(java.lang.String, char). That's more convenient than the one provided by Java out of the box if you don't need regular expressions. Like its manual says, it works like this:
最简单的方法是使用StringUtils#split(java.lang)。字符串,字符)。如果您不需要正则表达式,那么它比Java提供的那个更方便。就像手册上说的,它是这样工作的:
A null input String returns null.
StringUtils.split(null, *) = null
StringUtils.split("", *) = []
StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
StringUtils.split("a:b:c", '.') = ["a:b:c"]
StringUtils.split("a b c", ' ') = ["a", "b", "c"]
I would recommend using commong-lang, since usually it contains a lot of stuff that's usable. However, if you don't need it for anything else than doing a split, then implementing yourself or escaping the regex is a better option.
我建议使用commong-lang,因为它通常包含许多可用的东西。但是,如果您不需要它来做其他的事情,那么实现您自己或者摆脱regex是一个更好的选择。
#8
13
You can try like this also
你也可以试试这个。
String concatenated_String="hi^Hello";
String split_string_array[]=concatenated_String.split("\\^");
#9
13
Use org.apache.commons.lang.StringUtils' split method which can split strings based on the character or string you want to split.
使用org.apache.common .lang. stringutils的split方法,它可以根据您想要分割的字符或字符串来拆分字符串。
Method signature:
方法签名:
public static String[] split(String str, char separatorChar);
In your case, you want to split a string when there is a "-".
在您的情况中,您希望在有“-”时分割字符串。
You can simply do as follows:
你可以简单地做如下:
String str = "004-034556";
String split[] = StringUtils.split(str,"-");
Output:
输出:
004
034556
Assume that if -
does not exists in your string, it returns the given string, and you will not get any exception.
假设如果-不存在于您的字符串中,它将返回给定的字符串,您将不会得到任何异常。
#10
12
With Java 8:
与Java 8:
List<String> stringList = Pattern.compile("-")
.splitAsStream("004-034556")
.collect(Collectors.toList());
stringList.forEach(s -> System.out.println(s));
#11
10
String Split with multiple characters using Regex
使用正则表达式与多个字符分隔。
public class StringSplitTest {
public static void main(String args[]) {
String s = " ;String; String; String; String, String; String;;String;String; String; String; ;String;String;String;String";
//String[] strs = s.split("[,\\s\\;]");
String[] strs = s.split("[,\\;]");
System.out.println("Substrings length:"+strs.length);
for (int i=0; i < strs.length; i++) {
System.out.println("Str["+i+"]:"+strs[i]);
}
}
}
Output:
输出:
Substrings length:17
Str[0]:
Str[1]:String
Str[2]: String
Str[3]: String
Str[4]: String
Str[5]: String
Str[6]: String
Str[7]:
Str[8]:String
Str[9]:String
Str[10]: String
Str[11]: String
Str[12]:
Str[13]:String
Str[14]:String
Str[15]:String
Str[16]:String
But do not expect the same output across all JDK versions. I have seen one bug which exists in some JDK versions where the first null string has been ignored. This bug is not present in the latest JDK version, but it exists in some versions between JDK 1.7 late versions and 1.8 early versions.
但是不要期望在所有JDK版本中都有相同的输出。我已经看到了在一些JDK版本中存在的一个bug,其中第一个空字符串被忽略了。这个bug并没有出现在最新的JDK版本中,但是它存在于JDK 1.7晚期版本和1.8早期版本之间的一些版本中。
#12
10
For simple use cases String.split()
should do the job. If you use guava, there is also a Splitter class which allows chaining of different string operations and supports CharMatcher:
对于简单的用例字符串。split()应该做这个工作。如果您使用guava,还有一个Splitter类,它允许对不同的字符串操作进行链接,并支持CharMatcher:
Splitter.on('-')
.trimResults()
.omitEmptyStrings()
.split(string);
#13
9
public class SplitTest {
public static String[] split(String text, String delimiter) {
java.util.List<String> parts = new java.util.ArrayList<String>();
text += delimiter;
for (int i = text.indexOf(delimiter), j=0; i != -1;) {
String temp = text.substring(j,i);
if(temp.trim().length() != 0) {
parts.add(temp);
}
j = i + delimiter.length();
i = text.indexOf(delimiter,j);
}
return parts.toArray(new String[0]);
}
public static void main(String[] args) {
String str = "004-034556";
String delimiter = "-";
String result[] = split(str, delimiter);
for(String s:result)
System.out.println(s);
}
}
#14
8
You can split a string by a line break by using the following statement:
您可以使用以下语句将一个字符串拆分为一个行中断:
String textStr[] = yourString.split("\\r?\\n");
You can split a string by a hyphen/character by using the following statement:
您可以使用以下语句将字符串拆分为连字符/字符:
String textStr[] = yourString.split("-");
#15
8
import java.io.*;
public class BreakString {
public static void main(String args[]) {
String string = "004-034556-1234-2341";
String[] parts = string.split("-");
for(int i=0;i<parts.length;i++) {
System.out.println(parts[i]);
}
}
}
#16
7
The fastest way, which also consumes the least resource could be:
最快的方式,也消耗最少的资源可以是:
String s = "abc-def";
int p = s.indexOf('-');
if (p >= 0) {
String left = s.substring(0, p);
String right = s.substring(p + 1);
} else {
// s does not contain '-'
}
#17
6
One way to do this is to run through the String in a for-each loop and use the required split character.
一种方法是在for-each循环中运行字符串,并使用所需的split字符。
public class StringSplitTest {
public static void main(String[] arg){
String str = "004-034556";
String split[] = str.split("-");
System.out.println("The split parts of the String are");
for(String s:split)
System.out.println(s);
}
}
Output:
输出:
The split parts of the String are:
004
034556
#18
6
Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons, and its use is discouraged in new code. And we can make use of the split method as suggested by others as well.
请不要使用StringTokenizer类,因为它是由于兼容性原因而保留的遗留类,并且在新代码中不鼓励使用它。我们也可以用其他的方法来使用分割方法。
String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));
And as expected it will print:
正如预期的那样,它将印刷:
[004, 034556]
In this answer I also want to point out one change that has taken place for split
method in Java 8. The String#split() method makes use of Pattern.split
, and now it will remove empty strings at the start of the result array. Notice this change in documentation for Java 8:
在这个答案中,我还想指出在Java 8中发生的分割方法的一个变化。String#split()方法使用模式。拆分,现在它将在结果数组开始时删除空字符串。请注意Java 8的文档更改:
When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.
当输入序列的开头有正宽度匹配时,在结果数组的开头包含一个空的主要子字符串。一开始的零宽度匹配,但不会产生这样空的引导子字符串。
It means for the following example:
它的意思是:
String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));
we will get three strings: [0, 0, 4]
and not four as was the case in Java 7 and before. Also check this similar question.
我们将得到三个字符串:[0,0,4],而不是Java 7和之前的4个字符串。也要检查这个类似的问题。
#19
6
You can use Split():
您可以使用分裂():
import java.io.*;
public class Splitting
{
public static void main(String args[])
{
String Str = new String("004-034556");
String[] SplittoArray = Str.split("-");
String string1 = SplittoArray[0];
String string2 = SplittoArray[1];
}
}
Else, you can use StringTokenizer:
另外,你可以使用StringTokenizer:
import java.util.*;
public class Splitting
{
public static void main(String[] args)
{
StringTokenizer Str = new StringTokenizer("004-034556");
String string1 = Str.nextToken("-");
String string2 = Str.nextToken("-");
}
}
#20
5
Here are two ways two achieve it.
这里有两种方法实现它。
WAY 1: As you have to split two numbers by a special character you can use regex
方法一:当你必须用一个特殊字符分割两个数字时,你可以使用正则表达式。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TrialClass
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher("004-034556");
while(m.find())
{
System.out.println(m.group());
}
}
}
WAY 2: Using the string split method
方法2:使用字符串分割方法。
public class TrialClass
{
public static void main(String[] args)
{
String temp = "004-034556";
String [] arrString = temp.split("-");
for(String splitString:arrString)
{
System.out.println(splitString);
}
}
}
#21
4
You can simply use StringTokenizer to split a string in two or more parts whether there are any type of delimiters:
您可以简单地使用StringTokenizer在两个或多个部分拆分字符串,是否有任何类型的分隔符:
StringTokenizer st = new StringTokenizer("004-034556", "-");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
#22
3
Check out the split()
method in the String
class on javadoc.
在javadoc的String类中检查split()方法。
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html分裂(以)
String data = "004-034556-1212-232-232";
int cnt = 1;
for (String item : data.split("-")) {
System.out.println("string "+cnt+" = "+item);
cnt++;
}
Here many examples for split string but I little code optimized.
这里有很多拆分字符串的例子,但我没有优化代码。
#23
3
String str="004-034556"
String[] sTemp=str.split("-");// '-' is a delimiter
string1=004 // sTemp[0];
string2=034556//sTemp[1];
#24
2
To summarize: there are at least five ways to split a string in Java:
总结:在Java中至少有五种方法可以拆分字符串:
-
String.split():
String.split():
String[] parts ="10,20".split(",");
-
Pattern.compile(regexp).splitAsStream(input):
Pattern.compile(正则表达式).splitAsStream(输入):
List<String> strings = Pattern.compile("\\|") .splitAsStream("010|020202") .collect(Collectors.toList());
-
StringTokenizer (legacy class):
StringTokenizer(遗产类):
StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", "."); while(strings.hasMoreTokens()){ String substring = strings.nextToken(); System.out.println(substring); }
-
Google Guava Splitter:
谷歌番石榴分配器:
Iterable<String> result = Splitter.on(",").split("1,2,3,4");
-
Apache Commons StringUtils:
Apache Commons stringutil的:
String[] strings = StringUtils.split("1,2,3,4", ",");
So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).
因此,您可以根据需要选择最佳选项,例如返回类型(数组、列表或iterable)。
Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)
这里是对这些方法和最常见的例子的概述(如何用点、斜线、问号等进行拆分)
#25
0
String s="004-034556";
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='-')
{
System.out.println(s.substring(0,i));
System.out.println(s.substring(i+1));
}
}
As mentioned by everyone, split() is the best option which may be used in your case. An alternative method can be using substring().
正如每个人所提到的,split()是在您的案例中可能使用的最佳选项。另一种方法是使用子字符串()。
#26
0
To split a string, use String.split(regex)
:
要拆分字符串,请使用string .split(regex):
String phone = "004-034556";
String[] output = phone.split("-");
System.out.println(output[0]);
System.out.println(output[1]);
Output:
输出:
004
034556
#27
0
To split a string, uses String.split(regex). Review the following examples :
要拆分字符串,使用string .split(regex)。回顾以下例子:
String data = "004-034556";
String[] output = data.split("-");
System.out.println(output[0]);
System.out.println(output[1]);
Output
输出
004
034556
Note This split (regex) takes a regex as an argument, remember to escape the regex special characters, like period/dot.
注意,这个split (regex)以regex作为参数,记住要避开regex特殊字符,比如period/dot。
#28
-1
From the documentation:
从文档:
public String[] split(String regex,int limit)
Splits this string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.public String[] split(String regex,int limit)将这个字符串与给定正则表达式的匹配进行分割。该方法返回的数组包含该字符串的每个子字符串,该字符串由与给定表达式匹配的另一个子字符串终止,或者在字符串的末尾终止。数组中的子字符串按照它们在这个字符串中发生的顺序排列。如果表达式与输入的任何部分不匹配,那么得到的数组只有一个元素,即这个字符串。
Basically you can do something like this:
基本上你可以这样做:
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);
}
Output:
输出:
123
456
789
123
#29
-1
I just wanted to write an algorithm instead of using Java built-in functions:
我只是想写一个算法,而不是使用Java内置函数:
public static List<String> split(String str, char c){
List<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) != c){
sb.append(str.charAt(i));
}
else{
if(sb.length() > 0){
list.add(sb.toString());
sb = new StringBuilder();
}
}
}
if(sb.length() >0){
list.add(sb.toString());
}
return list;
}
#30
-2
String string = "004^034556-34";
String[] parts = string.split(Pattern.quote("^"));
If you have a special character then you can use Patter.quote. If you simply have dash (-) then you can shorten the code:
如果你有一个特殊的字符,那么你可以使用模式。如果你只需要破折号(-),那么你可以缩短代码:
String string = "004-34";
String[] parts = string.split("-");
If you try to add other special character in place of dash (^) then the error will generate ArrayIndexOutOfBoundsException. For that you have to use Pattern.quote
.
如果你想添加其他特殊字符的破折号(^),那么将产生ArrayIndexOutOfBoundsException的错误。因为你必须使用模式。
#1
2230
Just use the appropriate method: String#split()
.
只需使用适当的方法:String#split()。
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Note that this takes a regular expression, so remember to escape special characters if necessary.
注意,这需要一个正则表达式,所以如果需要,记得要避开特殊字符。
there are 12 characters with special meanings: the backslash
\
, the caret^
, the dollar sign$
, the period or dot.
, the vertical bar or pipe symbol|
, the question mark?
, the asterisk or star*
, the plus sign+
, the opening parenthesis(
, the closing parenthesis)
, and the opening square bracket[
, the opening curly brace{
, These special characters are often called "metacharacters".有12个字符的特殊含义:反斜杠\,插入符号^,美元符号$,周期或点,竖线或管道符号|,问号?,星号或* *,加号+,括号(关闭括号)开幕式,开幕式方括号[,左括号{,这些特殊字符通常被称为“元字符”。
So, if you want to split on e.g. period/dot .
which means "any character" in regex, use either backslash \
to escape the individual special character like so split("\\.")
, or use character class []
to represent literal character(s) like so split("[.]")
, or use Pattern#quote()
to escape the entire string like so split(Pattern.quote("."))
.
所以,如果你想在周期/点上分开。在regex中,它的意思是“任何字符”,使用反斜杠\以避免单独的特殊字符,如so split(“\\.”),或者使用字符类[]来表示文字字符,例如so split(“[.]”),或者使用Pattern#quote()来转义整个字符串,如so split(Pattern.quote(“。”))。
String[] parts = string.split(Pattern.quote(".")); // Split on period.
To test beforehand if the string contains certain character(s), just use String#contains()
.
要预先测试字符串是否包含某些字符,只需使用string # include()。
if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Note, this does not take a regular expression. For that, use String#matches()
instead.
注意,这不是一个正则表达式。为此,使用字符串#匹配()代替。
If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<=
group on the pattern.
如果你想保留在结果部分中的分割字符,然后使用积极的lookaround。如果你想让分裂的字符以左手边结束,请在模式上使用前缀?<=组。
String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556
In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?=
group on the pattern.
如果你想让这个分裂的字符在右边结束,那么在模式上使用前面的前缀?=组。
String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556
If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split()
method.
如果您想要限制产生的部分的数量,那么您可以提供所需的编号作为split()方法的第二个参数。
String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42
#2
65
An alternative to processing the string directly would be to use a regular expression with capturing groups. This has the advantage that it makes it straightforward to imply more sophisticated constraints on the input. For example, the following splits the string into two parts, and ensures that both consist only of digits:
直接处理字符串的另一种方法是使用带有捕获组的正则表达式。这样做的好处是,它可以直接地表示对输入的更复杂的约束。例如,下面将字符串拆分为两个部分,并确保它们都只包含数字:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class SplitExample
{
private static Pattern twopart = Pattern.compile("(\\d+)-(\\d+)");
public static void checkString(String s)
{
Matcher m = twopart.matcher(s);
if (m.matches()) {
System.out.println(s + " matches; first part is " + m.group(1) +
", second part is " + m.group(2) + ".");
} else {
System.out.println(s + " does not match.");
}
}
public static void main(String[] args) {
checkString("123-4567");
checkString("foo-bar");
checkString("123-");
checkString("-4567");
checkString("123-4567-890");
}
}
As the pattern is fixed in this instance, it can be compiled in advance and stored as a static member (initialised at class load time in the example). The regular expression is:
由于该模式在此实例中是固定的,因此可以预先编译并存储为静态成员(在示例中初始化在类加载时)。正则表达式是:
(\d+)-(\d+)
The parentheses denote the capturing groups; the string that matched that part of the regexp can be accessed by the Match.group() method, as shown. The \d matches and single decimal digit, and the + means "match one or more of the previous expression). The - has no special meaning, so just matches that character in the input. Note that you need to double-escape the backslashes when writing this as a Java string. Some other examples:
括号表示捕获组;与regexp部分匹配的字符串可以通过Match.group()方法访问,如所示。\d匹配和一个十进制数字,而+的意思是“匹配一个或多个前面的表达式)。没有特殊的含义,所以只匹配输入中的字符。注意,当将其作为Java字符串写入时,您需要双击反斜杠。一些其他的例子:
([A-Z]+)-([A-Z]+) // Each part consists of only capital letters
([^-]+)-([^-]+) // Each part consists of characters other than -
([A-Z]{2})-(\d+) // The first part is exactly two capital letters,
// the second consists of digits
#3
35
String[] result = yourString.split("-");
if (result.length != 2)
throw new IllegalArgumentException("String not in correct format");
This will split your string into 2 parts. The first element in the array will be the part containing the stuff before the -
, and the 2nd element in the array will contain the part of your string after the -
.
这将把你的字符串分成两部分。数组中的第一个元素将是包含前面内容的部分,数组中的第二个元素将包含字符串后面的部分。
If the array length is not 2, then the string was not in the format: string-string
.
如果数组长度不是2,那么字符串不是格式:字符串。
Check out the split()
method in the String
class.
检查String类中的split()方法。
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html # split-java.lang.String-int -
#4
25
// This leaves the regexes issue out of question
// But we must remember that each character in the Delimiter String is treated
// like a single delimiter
public static String[] SplitUsingTokenizer(String subject, String delimiters) {
StringTokenizer strTkn = new StringTokenizer(subject, delimiters);
ArrayList<String> arrLis = new ArrayList<String>(subject.length());
while(strTkn.hasMoreTokens())
arrLis.add(strTkn.nextToken());
return arrLis.toArray(new String[0]);
}
#5
21
String[] out = string.split("-");
should do thing you want. String class has many method to operate with string.
应该做你想做的事。String类有许多使用字符串操作的方法。
#6
16
The requirements left room for interpretation. I recommend writing a method,
这些要求留下了解释的余地。我建议写一种方法,
public final static String[] mySplit(final String s)
which encapsulate this function. Of course you can use String.split(..) as mentioned in the other answers for the implementation.
这个函数封装。当然,您可以使用String.split(..)作为实现的其他答案。
You should write some unit-tests for input strings and the desired results and behaviour.
您应该为输入字符串和期望的结果和行为编写一些单元测试。
Good test candidates should include:
优秀的应试者应该包括:
- "0022-3333"
- "-"
- "5555-"
- "-333"
- "3344-"
- "--"
- ""
- "553535"
- "333-333-33"
- "222--222"
- "222--"
- "--4555"
With defining the according test results, you can specify the behaviour.
通过定义测试结果,您可以指定行为。
For example, if "-333"
should return in [,333]
or if it is an error. Can "333-333-33"
be separated in [333,333-33] or [333-333,33]
or is it an error? And so on.
例如,如果“-333”应该返回[333],或者它是一个错误。“333- 3333 -33”可以在[3333 -33]或[333-333,33]中分开吗?还是一个错误?等等。
#7
14
Assuming, that
假设,
- you don't really need regular expressions for your split
- 你并不是真的需要正则表达式来进行拆分。
- you happen to already use apache commons lang in your app
- 你已经在app中使用了apache commons lang。
The easiest way is to use StringUtils#split(java.lang.String, char). That's more convenient than the one provided by Java out of the box if you don't need regular expressions. Like its manual says, it works like this:
最简单的方法是使用StringUtils#split(java.lang)。字符串,字符)。如果您不需要正则表达式,那么它比Java提供的那个更方便。就像手册上说的,它是这样工作的:
A null input String returns null.
StringUtils.split(null, *) = null
StringUtils.split("", *) = []
StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
StringUtils.split("a:b:c", '.') = ["a:b:c"]
StringUtils.split("a b c", ' ') = ["a", "b", "c"]
I would recommend using commong-lang, since usually it contains a lot of stuff that's usable. However, if you don't need it for anything else than doing a split, then implementing yourself or escaping the regex is a better option.
我建议使用commong-lang,因为它通常包含许多可用的东西。但是,如果您不需要它来做其他的事情,那么实现您自己或者摆脱regex是一个更好的选择。
#8
13
You can try like this also
你也可以试试这个。
String concatenated_String="hi^Hello";
String split_string_array[]=concatenated_String.split("\\^");
#9
13
Use org.apache.commons.lang.StringUtils' split method which can split strings based on the character or string you want to split.
使用org.apache.common .lang. stringutils的split方法,它可以根据您想要分割的字符或字符串来拆分字符串。
Method signature:
方法签名:
public static String[] split(String str, char separatorChar);
In your case, you want to split a string when there is a "-".
在您的情况中,您希望在有“-”时分割字符串。
You can simply do as follows:
你可以简单地做如下:
String str = "004-034556";
String split[] = StringUtils.split(str,"-");
Output:
输出:
004
034556
Assume that if -
does not exists in your string, it returns the given string, and you will not get any exception.
假设如果-不存在于您的字符串中,它将返回给定的字符串,您将不会得到任何异常。
#10
12
With Java 8:
与Java 8:
List<String> stringList = Pattern.compile("-")
.splitAsStream("004-034556")
.collect(Collectors.toList());
stringList.forEach(s -> System.out.println(s));
#11
10
String Split with multiple characters using Regex
使用正则表达式与多个字符分隔。
public class StringSplitTest {
public static void main(String args[]) {
String s = " ;String; String; String; String, String; String;;String;String; String; String; ;String;String;String;String";
//String[] strs = s.split("[,\\s\\;]");
String[] strs = s.split("[,\\;]");
System.out.println("Substrings length:"+strs.length);
for (int i=0; i < strs.length; i++) {
System.out.println("Str["+i+"]:"+strs[i]);
}
}
}
Output:
输出:
Substrings length:17
Str[0]:
Str[1]:String
Str[2]: String
Str[3]: String
Str[4]: String
Str[5]: String
Str[6]: String
Str[7]:
Str[8]:String
Str[9]:String
Str[10]: String
Str[11]: String
Str[12]:
Str[13]:String
Str[14]:String
Str[15]:String
Str[16]:String
But do not expect the same output across all JDK versions. I have seen one bug which exists in some JDK versions where the first null string has been ignored. This bug is not present in the latest JDK version, but it exists in some versions between JDK 1.7 late versions and 1.8 early versions.
但是不要期望在所有JDK版本中都有相同的输出。我已经看到了在一些JDK版本中存在的一个bug,其中第一个空字符串被忽略了。这个bug并没有出现在最新的JDK版本中,但是它存在于JDK 1.7晚期版本和1.8早期版本之间的一些版本中。
#12
10
For simple use cases String.split()
should do the job. If you use guava, there is also a Splitter class which allows chaining of different string operations and supports CharMatcher:
对于简单的用例字符串。split()应该做这个工作。如果您使用guava,还有一个Splitter类,它允许对不同的字符串操作进行链接,并支持CharMatcher:
Splitter.on('-')
.trimResults()
.omitEmptyStrings()
.split(string);
#13
9
public class SplitTest {
public static String[] split(String text, String delimiter) {
java.util.List<String> parts = new java.util.ArrayList<String>();
text += delimiter;
for (int i = text.indexOf(delimiter), j=0; i != -1;) {
String temp = text.substring(j,i);
if(temp.trim().length() != 0) {
parts.add(temp);
}
j = i + delimiter.length();
i = text.indexOf(delimiter,j);
}
return parts.toArray(new String[0]);
}
public static void main(String[] args) {
String str = "004-034556";
String delimiter = "-";
String result[] = split(str, delimiter);
for(String s:result)
System.out.println(s);
}
}
#14
8
You can split a string by a line break by using the following statement:
您可以使用以下语句将一个字符串拆分为一个行中断:
String textStr[] = yourString.split("\\r?\\n");
You can split a string by a hyphen/character by using the following statement:
您可以使用以下语句将字符串拆分为连字符/字符:
String textStr[] = yourString.split("-");
#15
8
import java.io.*;
public class BreakString {
public static void main(String args[]) {
String string = "004-034556-1234-2341";
String[] parts = string.split("-");
for(int i=0;i<parts.length;i++) {
System.out.println(parts[i]);
}
}
}
#16
7
The fastest way, which also consumes the least resource could be:
最快的方式,也消耗最少的资源可以是:
String s = "abc-def";
int p = s.indexOf('-');
if (p >= 0) {
String left = s.substring(0, p);
String right = s.substring(p + 1);
} else {
// s does not contain '-'
}
#17
6
One way to do this is to run through the String in a for-each loop and use the required split character.
一种方法是在for-each循环中运行字符串,并使用所需的split字符。
public class StringSplitTest {
public static void main(String[] arg){
String str = "004-034556";
String split[] = str.split("-");
System.out.println("The split parts of the String are");
for(String s:split)
System.out.println(s);
}
}
Output:
输出:
The split parts of the String are:
004
034556
#18
6
Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons, and its use is discouraged in new code. And we can make use of the split method as suggested by others as well.
请不要使用StringTokenizer类,因为它是由于兼容性原因而保留的遗留类,并且在新代码中不鼓励使用它。我们也可以用其他的方法来使用分割方法。
String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));
And as expected it will print:
正如预期的那样,它将印刷:
[004, 034556]
In this answer I also want to point out one change that has taken place for split
method in Java 8. The String#split() method makes use of Pattern.split
, and now it will remove empty strings at the start of the result array. Notice this change in documentation for Java 8:
在这个答案中,我还想指出在Java 8中发生的分割方法的一个变化。String#split()方法使用模式。拆分,现在它将在结果数组开始时删除空字符串。请注意Java 8的文档更改:
When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.
当输入序列的开头有正宽度匹配时,在结果数组的开头包含一个空的主要子字符串。一开始的零宽度匹配,但不会产生这样空的引导子字符串。
It means for the following example:
它的意思是:
String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));
we will get three strings: [0, 0, 4]
and not four as was the case in Java 7 and before. Also check this similar question.
我们将得到三个字符串:[0,0,4],而不是Java 7和之前的4个字符串。也要检查这个类似的问题。
#19
6
You can use Split():
您可以使用分裂():
import java.io.*;
public class Splitting
{
public static void main(String args[])
{
String Str = new String("004-034556");
String[] SplittoArray = Str.split("-");
String string1 = SplittoArray[0];
String string2 = SplittoArray[1];
}
}
Else, you can use StringTokenizer:
另外,你可以使用StringTokenizer:
import java.util.*;
public class Splitting
{
public static void main(String[] args)
{
StringTokenizer Str = new StringTokenizer("004-034556");
String string1 = Str.nextToken("-");
String string2 = Str.nextToken("-");
}
}
#20
5
Here are two ways two achieve it.
这里有两种方法实现它。
WAY 1: As you have to split two numbers by a special character you can use regex
方法一:当你必须用一个特殊字符分割两个数字时,你可以使用正则表达式。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TrialClass
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher("004-034556");
while(m.find())
{
System.out.println(m.group());
}
}
}
WAY 2: Using the string split method
方法2:使用字符串分割方法。
public class TrialClass
{
public static void main(String[] args)
{
String temp = "004-034556";
String [] arrString = temp.split("-");
for(String splitString:arrString)
{
System.out.println(splitString);
}
}
}
#21
4
You can simply use StringTokenizer to split a string in two or more parts whether there are any type of delimiters:
您可以简单地使用StringTokenizer在两个或多个部分拆分字符串,是否有任何类型的分隔符:
StringTokenizer st = new StringTokenizer("004-034556", "-");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
#22
3
Check out the split()
method in the String
class on javadoc.
在javadoc的String类中检查split()方法。
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html分裂(以)
String data = "004-034556-1212-232-232";
int cnt = 1;
for (String item : data.split("-")) {
System.out.println("string "+cnt+" = "+item);
cnt++;
}
Here many examples for split string but I little code optimized.
这里有很多拆分字符串的例子,但我没有优化代码。
#23
3
String str="004-034556"
String[] sTemp=str.split("-");// '-' is a delimiter
string1=004 // sTemp[0];
string2=034556//sTemp[1];
#24
2
To summarize: there are at least five ways to split a string in Java:
总结:在Java中至少有五种方法可以拆分字符串:
-
String.split():
String.split():
String[] parts ="10,20".split(",");
-
Pattern.compile(regexp).splitAsStream(input):
Pattern.compile(正则表达式).splitAsStream(输入):
List<String> strings = Pattern.compile("\\|") .splitAsStream("010|020202") .collect(Collectors.toList());
-
StringTokenizer (legacy class):
StringTokenizer(遗产类):
StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", "."); while(strings.hasMoreTokens()){ String substring = strings.nextToken(); System.out.println(substring); }
-
Google Guava Splitter:
谷歌番石榴分配器:
Iterable<String> result = Splitter.on(",").split("1,2,3,4");
-
Apache Commons StringUtils:
Apache Commons stringutil的:
String[] strings = StringUtils.split("1,2,3,4", ",");
So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).
因此,您可以根据需要选择最佳选项,例如返回类型(数组、列表或iterable)。
Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)
这里是对这些方法和最常见的例子的概述(如何用点、斜线、问号等进行拆分)
#25
0
String s="004-034556";
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='-')
{
System.out.println(s.substring(0,i));
System.out.println(s.substring(i+1));
}
}
As mentioned by everyone, split() is the best option which may be used in your case. An alternative method can be using substring().
正如每个人所提到的,split()是在您的案例中可能使用的最佳选项。另一种方法是使用子字符串()。
#26
0
To split a string, use String.split(regex)
:
要拆分字符串,请使用string .split(regex):
String phone = "004-034556";
String[] output = phone.split("-");
System.out.println(output[0]);
System.out.println(output[1]);
Output:
输出:
004
034556
#27
0
To split a string, uses String.split(regex). Review the following examples :
要拆分字符串,使用string .split(regex)。回顾以下例子:
String data = "004-034556";
String[] output = data.split("-");
System.out.println(output[0]);
System.out.println(output[1]);
Output
输出
004
034556
Note This split (regex) takes a regex as an argument, remember to escape the regex special characters, like period/dot.
注意,这个split (regex)以regex作为参数,记住要避开regex特殊字符,比如period/dot。
#28
-1
From the documentation:
从文档:
public String[] split(String regex,int limit)
Splits this string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.public String[] split(String regex,int limit)将这个字符串与给定正则表达式的匹配进行分割。该方法返回的数组包含该字符串的每个子字符串,该字符串由与给定表达式匹配的另一个子字符串终止,或者在字符串的末尾终止。数组中的子字符串按照它们在这个字符串中发生的顺序排列。如果表达式与输入的任何部分不匹配,那么得到的数组只有一个元素,即这个字符串。
Basically you can do something like this:
基本上你可以这样做:
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);
}
Output:
输出:
123
456
789
123
#29
-1
I just wanted to write an algorithm instead of using Java built-in functions:
我只是想写一个算法,而不是使用Java内置函数:
public static List<String> split(String str, char c){
List<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) != c){
sb.append(str.charAt(i));
}
else{
if(sb.length() > 0){
list.add(sb.toString());
sb = new StringBuilder();
}
}
}
if(sb.length() >0){
list.add(sb.toString());
}
return list;
}
#30
-2
String string = "004^034556-34";
String[] parts = string.split(Pattern.quote("^"));
If you have a special character then you can use Patter.quote. If you simply have dash (-) then you can shorten the code:
如果你有一个特殊的字符,那么你可以使用模式。如果你只需要破折号(-),那么你可以缩短代码:
String string = "004-34";
String[] parts = string.split("-");
If you try to add other special character in place of dash (^) then the error will generate ArrayIndexOutOfBoundsException. For that you have to use Pattern.quote
.
如果你想添加其他特殊字符的破折号(^),那么将产生ArrayIndexOutOfBoundsException的错误。因为你必须使用模式。