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
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
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
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
The string you give
split
is the string form of a regular expression, so: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[]
.)You can use the regex "\W".This matches any non-word character.The required line would be:
should work.
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.Refer to the following snippet:
You may also specified regular expression as argument in split() method ..see below example....