How To Split String by space and store each character or substring in array
I have: String – '(1 OR 2) AND 3'
I need in this way:
String [] filterLogicSplittedbySpace=new String[]{'(', '1', 'OR', '2', ')', 'AND', '3'};
Please guide.
How To Split String by space and store each character or substring in array
I have: String – '(1 OR 2) AND 3'
I need in this way:
String [] filterLogicSplittedbySpace=new String[]{'(', '1', 'OR', '2', ')', 'AND', '3'};
Please guide.
Best Answer
Usage of
String.split
method doesn't fit your requirements, as you will have'(1'
first element and'2)'
third element. Use String.splitByCharacterType method.This will split and extract correct parts of your string. Brackets are split from numbers
Debug result is:
this code also works for more complicated conditions, like
String str = '((1 OR 2) AND 3) OR 4';
Debug is: