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
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='';