+ Start a Discussion
Radhe Shyam.Radhe Shyam. 

How To Split String by space and store each character or substring in array

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 chosen by Radhe Shyam.
Sandeep WaliaSandeep Walia
Hi Radhe,

As per your example, you are not exactly splitting the string by space.Please consider the below code that splits strings by space and saves it in a list of string.
String s = '(1 OR 2) AND 3';
List<String> filterLogicSplittedbySpace = s.split(' ');
for (String str : filterLogicSplittedbySpace){
    system.debug(str);
}

It provides the following output:
User-added image
The string '(1' and '2)' will be together based if we split based on space whereas in your example they are in different strings.

Hope this helps,
Sandeep​

All Answers

Sandeep WaliaSandeep Walia
Hi Radhe,

As per your example, you are not exactly splitting the string by space.Please consider the below code that splits strings by space and saves it in a list of string.
String s = '(1 OR 2) AND 3';
List<String> filterLogicSplittedbySpace = s.split(' ');
for (String str : filterLogicSplittedbySpace){
    system.debug(str);
}

It provides the following output:
User-added image
The string '(1' and '2)' will be together based if we split based on space whereas in your example they are in different strings.

Hope this helps,
Sandeep​
This was selected as the best answer
Radhe Shyam.Radhe Shyam.
Hi Sandeep, 

It works for me. I am Posting another Question  :https://developer.salesforce.com/forums?id=9060G000000BiKCQA0