I need to split a string base on delimiter -
and .
. Below are my desired output.
我需要在分隔符的基础上分割一个字符串。下面是我想要的输出。
AA.BB-CC-DD.zip
->
AA.BB-CC-DD。zip - >
AA
BB
CC
DD
zip
but my following code does not work.
但是我下面的代码不起作用。
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
11 个解决方案
#1
248
I think you need to include the regex OR operator:
我认为您需要包含regex或操作符:
String[]tokens = pdfName.split("-|\\.");
What you have will match "-." not a "-" or a "."
你所拥有的将是匹配的"-."而不是"-"或" a "。"
#2
42
Try this regex "[-.]+"
. The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
试试这个正则表达式“(-)+”。+将连续分隔符chars视为一个。如果你不想要这个,就删除它。
#3
23
You can use the regex "\W".This matches any non-word character.The required line would be:
您可以使用regex“\W”。这匹配任何非文字字符。所需的线是:
String[] tokens=pdfName.split("\\W");
#4
11
Using Guava you could do this:
你可以用番石榴:
Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);
#5
10
The string you give split
is the string form of a regular expression, so:
你给split的字符串是正则表达式的字符串形式,因此:
private void getId(String pdfName){
String[]tokens = pdfName.split("[\\-.]");
}
That means to split on any character in the []
(we have to escape -
with a backslash because it's special inside []
; and of course we have to escape the backslash because this is a string). (Conversely, .
is normally special but isn't special inside []
.)
这意味着在[]中分割任何字符(我们必须转义——使用反斜杠,因为它在[]内部是特殊的;当然,我们必须转义反斜杠,因为这是一个字符串)。(相反的,。通常是特殊的,但在内部并不特殊。
#6
3
I'd use Apache Commons:
我使用Apache Commons:
import org.apache.commons.lang3.StringUtils;
进口org.apache.commons.lang3.StringUtils;
private void getId(String pdfName){
String[] tokens = StringUtils.split(pdfName, "-.");
}
It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator)
which uses the complete string as a separator
它将在任何指定的分隔符上分割,而不是StringUtils。splitbyproduceparator (str,分隔符),它使用完整的字符串作为分隔符。
#7
0
You may also specified regular expression as argument in split() method ..see below example....
您还可以在split()方法中将正则表达式指定为参数。参见下面的例子....
private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}
#8
0
It's better to use something like this:
最好是这样:
s.split("[\\s\\-\\.\\'\\?\\,\\_\\@]+");
Have added a few other characters as sample. This is the safest way to use, because the way .
and '
is treated.
添加了一些其他字符作为示例。这是最安全的使用方法,因为这个方法。和“治疗。
#9
0
s.trim().split("[\\W]+")
should work.
应该工作。
#10
0
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
对于两个字符序列,如“delimeter”和“AND”或“this should be working”。使用时别忘了修剪。
String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
String[] cities = text.split("AND|OR");
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
结果:城市={“伊斯坦布尔”、“纽约”、“巴黎”、“东京”、“莫斯科”}
#11
-1
If you know the sting will always be in the same format, first split the string based on .
and store the string at the first index in a variable. Then split the string in the second index based on -
and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on .
and you should have obtained all of the relevant fields.
如果您知道sting将始终保持相同的格式,那么首先根据这个格式拆分字符串。并将字符串存储在变量的第一个索引中。然后根据-和存储索引0,1和2将字符串分割到第二个索引中。最后,根据前面的数组分割索引2。你应该得到所有相关的字段。
Refer to the following snippet:
请参阅以下片段:
String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...
#1
248
I think you need to include the regex OR operator:
我认为您需要包含regex或操作符:
String[]tokens = pdfName.split("-|\\.");
What you have will match "-." not a "-" or a "."
你所拥有的将是匹配的"-."而不是"-"或" a "。"
#2
42
Try this regex "[-.]+"
. The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
试试这个正则表达式“(-)+”。+将连续分隔符chars视为一个。如果你不想要这个,就删除它。
#3
23
You can use the regex "\W".This matches any non-word character.The required line would be:
您可以使用regex“\W”。这匹配任何非文字字符。所需的线是:
String[] tokens=pdfName.split("\\W");
#4
11
Using Guava you could do this:
你可以用番石榴:
Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);
#5
10
The string you give split
is the string form of a regular expression, so:
你给split的字符串是正则表达式的字符串形式,因此:
private void getId(String pdfName){
String[]tokens = pdfName.split("[\\-.]");
}
That means to split on any character in the []
(we have to escape -
with a backslash because it's special inside []
; and of course we have to escape the backslash because this is a string). (Conversely, .
is normally special but isn't special inside []
.)
这意味着在[]中分割任何字符(我们必须转义——使用反斜杠,因为它在[]内部是特殊的;当然,我们必须转义反斜杠,因为这是一个字符串)。(相反的,。通常是特殊的,但在内部并不特殊。
#6
3
I'd use Apache Commons:
我使用Apache Commons:
import org.apache.commons.lang3.StringUtils;
进口org.apache.commons.lang3.StringUtils;
private void getId(String pdfName){
String[] tokens = StringUtils.split(pdfName, "-.");
}
It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator)
which uses the complete string as a separator
它将在任何指定的分隔符上分割,而不是StringUtils。splitbyproduceparator (str,分隔符),它使用完整的字符串作为分隔符。
#7
0
You may also specified regular expression as argument in split() method ..see below example....
您还可以在split()方法中将正则表达式指定为参数。参见下面的例子....
private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}
#8
0
It's better to use something like this:
最好是这样:
s.split("[\\s\\-\\.\\'\\?\\,\\_\\@]+");
Have added a few other characters as sample. This is the safest way to use, because the way .
and '
is treated.
添加了一些其他字符作为示例。这是最安全的使用方法,因为这个方法。和“治疗。
#9
0
s.trim().split("[\\W]+")
should work.
应该工作。
#10
0
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
对于两个字符序列,如“delimeter”和“AND”或“this should be working”。使用时别忘了修剪。
String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
String[] cities = text.split("AND|OR");
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
结果:城市={“伊斯坦布尔”、“纽约”、“巴黎”、“东京”、“莫斯科”}
#11
-1
If you know the sting will always be in the same format, first split the string based on .
and store the string at the first index in a variable. Then split the string in the second index based on -
and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on .
and you should have obtained all of the relevant fields.
如果您知道sting将始终保持相同的格式,那么首先根据这个格式拆分字符串。并将字符串存储在变量的第一个索引中。然后根据-和存储索引0,1和2将字符串分割到第二个索引中。最后,根据前面的数组分割索引2。你应该得到所有相关的字段。
Refer to the following snippet:
请参阅以下片段:
String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...