I have a string like
abababa:nsndnfnng.leleelld_kdjdh
I want to split it on ":" and ".", so that I get a list as follows:
{abababa, nsndnfnng, eleelld_kdjdh}
how can I do this with calling the split() once?
I have a string like
abababa:nsndnfnng.leleelld_kdjdh
I want to split it on ":" and ".", so that I get a list as follows:
{abababa, nsndnfnng, eleelld_kdjdh}
how can I do this with calling the split() once?
You are looking for String#split
method. Since it accepts regex which will describe delimiter your code could look like
String[] result = yourString.split("[:.]");
You can just use String.split("[:.]")
which takes a regex argument
Common pitfall If you would only want to split on .
alone you have to escape the dot String.split("\\.")
(or use a character class here too String.split("[.]")
)
use regex and split the string
Example:
public static void main(String[] args) {
String REGEX_PATTERN = "[:.\\_]";
String s1 = "abababa:nsndnfnng.leleelld_kdjdh";
String[] result = s1.split(REGEX_PATTERN);
for (String myString : result) {
System.out.println(myString);
}
}