function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DileepDileep 

string split based on specified character

Hi All, 

I want to split a string based on the character on the string lets say(B, and F)

for example i want to break the string "B001B0002B000005F0006" to a string array like  this {"B001", "B0002", "B000005", "F0006" }

 

how can i do this

hitesh90hitesh90

Hi Dilu,

 

Try below code for your requirement.

 

string strYourstring = 'B001B0002B000005F0006';
strYourstring = strYourstring.replace('B',',B');
strYourstring = strYourstring.replace('F',',F');
string[] arrExpectedString = strYourstring.split(',');
system.debug('##########'+arrExpectedString);

 

 

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

 

Thank You,

Hitesh Patel

SFDC Certified Developer & Administrator

Raj.ax1558Raj.ax1558

Hello Bro,

Nice question.
I have implemented you question and found the solution. Try this code -

string str = 'B001B0002B000005F0006';
list<string> B = new list<string>();
list<string> F = new list<string>();
for(string s : str.split('B'))
{
      if(s.contains('F'))
       {
            F = s.split('F');
            system.debug('### F # - '+ s.split('F'));
        }
        B.add(s);
        system.debug('### S # - '+ s);
}

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Raj Jha