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
Nageswara  reddyNageswara reddy 

String Split method is not working in apex class

HI all,

 

  String method is not  working  in my apex class  ,

 I have written in my apex class like this

 

String address=add2;
string[] spilt= address.split(';');           
for(string s: spilt)
{
system.debug('***********'+s);

}

 Its  Not spiltting,

 

 Bur same code  when run in  console it will  work

 

String address='Abbyville,null,Kansas,92274|1520 Malley Road,Anchorage,Alaska,99501|';
string[] spilt= address.split(';');
for(string s: spilt)
{
system.debug('***********'+s);
}

 can any one help y it is not spiltting

 

Thanks

Nageswar

 

 

AdrianCCAdrianCC

Hi,

 

Put a debug  after "String address=add2;" to see what value it has. Maybe the string is empty.

 

Regards,

Adrian 

PrakashbPrakashb

Hi

 

you are aplitting it based on ';'.

 

On your strin you just have one ';' so you will be getting the entire string.

 

Do the splitting based on ','.

Nageswara  reddyNageswara reddy

thanks prakash,

 actullay  the string  contain two adresses that are separated by semi colon.   i want split the address es with semi coln

Nageswara  reddyNageswara reddy

Hi  i also put debug  , but string  is not empty   it contains "Abbyville,null,Kansas,92274|1520 Malley Road,Anchorage,Alaska,99501|'  " thisw value. In the string we have two adresses , they are separated them by  semi colon

AdrianCCAdrianCC

Really weird... if it works in the console why wouldn't it work inside your class??

Try escaping the ;.

String[] spilt = address.split('\\;'); 

Nageswara  reddyNageswara reddy

HI adrin,

 I tried  in that Manner also,  it is not  splitting .By defaultly it takes  orginal string.

Jerun JoseJerun Jose
Can you post the code of the class and how you are calling the method to do the string split?
Starz26Starz26

In addition, can you post the code or process as to how the add2 is constructed....