Split or tokenise a java String using a substring as delimiter - java

How to split or tokenise a String in java not based on regex but based on a substring?
String str = "{A={111={i=[a,b,c],ii=[e,f]}, 222={iii=[a,e]}}, B={333={i= [b,c]}}};
Now I want to tokenise or split the string based on substring "}}," and not regex "}},".

Although the String.split(String regex) function specifies that it takes a regular expression as a parameter, that does not stop you from escaping any special characters and splitting on a literal string.
To escape special characters in a regular expression, you can make use of the Pattern.quote(String s) function, or you can escape the individual characters using backslashes \\:
String escapedStr = Pattern.quote("}},");
String alternativeEscapedStr = "\\}\\},";
For the example you have provided however, you shouldn't need to escape anything:
String str = "{A={111={i=[a,b,c],ii=[e,f]}, 222={iii=[a,e]}}, B={333={i= [b,c]}}}";
String[] splitStr = str.split(Pattern.quote("}},"));
System.out.println(Arrays.toString(splitStr));
String[] splitStr2 = str.split("}},");
System.out.println(Arrays.toString(splitStr2));
Output:
[{A={111={i=[a,b,c],ii=[e,f]}, 222={iii=[a,e], B={333={i= [b,c]}}}]
[{A={111={i=[a,b,c],ii=[e,f]}, 222={iii=[a,e], B={333={i= [b,c]}}}]

String str = "{A={111={i=[a,b,c],ii=[e,f]}, 222={iii=[a,e]}}, B={333={i= [b,c]}}}";
String[] split = str.trim().split("}},");
Arrays.stream(split).forEach(s-> System.out.println(s));

Related

Split string against some characters except the # character

I want to split a string against the following characters
~!#$%^&*()_+­=<>,.?/:;"'{}|[]\, \n,\t, space
I tried to use \\s regex delimiter but i don't want the # included as the split character so that a string like this is #funny should result to this is #funny as the resulting values.
I have tried the following but it doesn't work.
this is #funny".split("\\s")
but it doesn't work. Any ideas?
Just specify the characters you want in square bracket, which means any of. Single escape Java characters (like \") and double escape Regex special characters (like \\[):
#Test
public void testName() throws Exception
{
String[] split = "this is #funny".split("[~!#$%^&*()_+­=<>,.?/:;\"'{}|\\[\\]\\\\ \\n\\t]");
for (String string : split)
{
logger.debug(string);
}
}
User replaceAll(String regex,String replacement) method from String.
String result = "this is #funny".replaceAll("[~!#$%^&*()_+­=<>,.?/:;\"'{}|\\[\\]\\,\\n\\t]", "");
System.out.println(result);
You can try to implement this:
String[] split = "this&is%a#funny^string".split("[^#\\p{Alnum}]|\\s+");
for (String string : split){
System.out.println(string);
}
Also check the Java API (Patterns) for more information on how to process strings.
It look like this will work for you:
String[] split = str.split("[^a-zA-Z&&[^#]]+");
This uses a character class subtraction to split on non-letter chars, except the hash.
Here's some test code:
String str = "this is #funny";
String[] split = str.split("[^a-zA-Z&&[^#]]+");
System.out.println(Arrays.toString(split));
Output:
[this, is, #funny]

String.split not working with combination of delimiter {^

I am trying to split the string with combination of {^
How to use combination of delimiter for splitting the string.
The sample data is :
String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";
The delimiter passed to String.split() is a regex. As { and ^ are characters with special meaning within a regex, you need to escape them if you want to use them as literals:
String[] tokens = str.split("\\{\\^");
split method in java takes an regex as an input.
so if you want to split the string using '{' and '^' then you need to do the following:
String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";
String[] splitted = str.split("\\{\\^"); //note \\ before { and ^
You have to escape { and ^ in your split Statement, because both are Special character in regex:
s.split("\\{\\^");

Java regex to split a string by using different delimiters

Suppose I want to split a string by either space character or the %20 string, how should I write my regex?
I tried the following, but it didn't work.
String regex = "[\\s+, %20]";
String str1 = "abc%20xyz";
String str2 = "abc xyz";
str1.split(regex);
str2.split(regex);
The regex doesn't seem to work on str1.
use the alternation |:
String regex = "(?:\\s+|%20)+";
String regex = "(\\s{1}+|%20{1}+)";
If you want to split by ONE space or ONE "%20", try this:
String regex = "(\\s|%20)";
If you want to split by AT LEAST ONE space or AT LEAST ONE "%20", then try this:
String regex = "(\\s+|(%20)+)";

How can I split a string by two delimiters?

I know that you can split your string using myString.split("something"). But I do not know how I can split a string by two delimiters.
Example:
mySring = "abc==abc++abc==bc++abc";
I need something like this:
myString.split("==|++")
What is its regularExpression?
Use this :
myString.split("(==)|(\\+\\+)")
How I would do it if I had to split using two substrings:
String mainString = "This is a dummy string with both_spaces_and_underscores!"
String delimiter1 = " ";
String delimiter2 = "_";
mainString = mainString.replaceAll(delimiter2, delimiter1);
String[] split_string = mainString.split(delimiter1);
Replace all instances of second delimiter with first and split with first.
Note: using replaceAll allows you to use regexp for delimiter2. So, you should actually replace all matches of delimiter2 with some string that matches delimiter1's regexp.
You can use this
mySring = "abc==abc++abc==bc++abc";
String[] splitString = myString.split("\\W+");
Regular expression \W+ ---> it will split the string based upon non-word character.
Try this
String str = "aa==bb++cc";
String[] split = str.split("={2}|\\+{2}");
System.out.println(Arrays.toString(split));
The answer is an array of
[aa, bb, cc]
The {2} matches two characters of the proceding character. That is either = or + (escaped)
The | matches either side
I am escaping the \ in java so the regex is actually ={2}|\+{2}

How to split the string using '^' this special character in java?

I want to split the following string "Good^Evening" i used split option it is not split the value. please help me.
This is what I've been trying:
String Val = "Good^Evening";
String[] valArray = Val.Split("^");
I'm assuming you did something like:
String[] parts = str.split("^");
That doesn't work because the argument to split is actually a regular expression, where ^ has a special meaning. Try this instead:
String[] parts = str.split("\\^");
The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning".
The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.
String[] parts = "Good^Evening".split("\\^");
The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.
try this
String str = "Good^Evening";
String newStr = str.replaceAll("[^]+", "");

Categories