+ Start a Discussion
mariappangomathirajmariappangomathiraj 

Problem in Replace in string Method

String Names = '^Community$|^Luxury$|^Golf$|^Course$|^Hospitality$|^Industrial$|^';

 

 

 

My Code:

 

Names = (Names!= '' && Names!= null) ? Names.replaceAll('\\$|^,'') : '';
system.debug(Logginglevel.Info,'::::Names::::::::::'+Names);

 

Current output in Debug Log:

^Community$|^Luxury$|^Golf$|^Course$|^Hospitality$|^Industrial$|^Industrial$|

 

Expected  Output:

 

Community,Luxury,Golf,Course,Hospitality,Industrial 

 

Best Answer chosen by Admin (Salesforce Developers) 
s_k_as_k_a

 

Use ResEx in replaceall function like below


Names = (Names!= '' && Names!= null) ? Names.replaceAll('[$|^]','') : '';

All Answers

s_k_as_k_a

 

Use ResEx in replaceall function like below


Names = (Names!= '' && Names!= null) ? Names.replaceAll('[$|^]','') : '';

This was selected as the best answer
VidulaVidula
Hi s_k_a
I have tried your code, It gives following output :
CommunityLuxuryGolfCourseHospitalityIndustrial
VidulaVidula

Hi,

try following, It may help you.

 

String Names = '^Community$|^Luxury$|^Golf$|^Course$|^Hospitality$|^Industrial$|^';


Names = (Names!= '' && Names!= null) ? Names.replace('$|^',',') : '';
Names = (Names!= '' && Names!= null) ? Names.replace('^','') : '';


system.debug('::::Names::::::::::'+Names);

 

I got followimg output :

 

Community,Luxury,Golf,Course,Hospitality,Industrial,

s_k_as_k_a

We can not get your required out put in one step.

 

Try below code for exact output what you want to get.

 

String Names = '^Community$|^Luxury$|^Golf$|^Course$|^Hospitality$|^Industrial$|^';

if(Names!= '' && Names!= null)

{

   Names = Names.remove('^') ;
   names = names.remove('$');
   names = names.replace('|',',');
  names = names.removeEnd(',');
  system.debug(Logginglevel.Info,'::::Names::::::::::'+Names);

}else

names='';