+ Start a Discussion
Raj R.Raj R. 

How to replace string commas from a string using replaceAll()?

Hi,
I have a class that is grabs Opportunities, places them into a csv file, and then sends an email to a particular email address. 

There is a Opportunity.CustomField1 which is a text field that can is populated as such:
  • Test, Test, Test,
  • Test; Test; Test;
Because I am creating csv file the string cannot have a comma in it other it throws off the csv file.

I tried doing this but failed

String x = Op.CustomField1;
x.replaceAll(',', ';');

Essentially when the CustomField1 has semicolon (;) character everything works fine, but when there is a comma (,) to separate the values, then the CSV file is messed up because the columns are not aligned properly.

How can I use x.replaceAll() to replace all commas with a semicolon in the customField1?
Best Answer chosen by Raj R.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Use it like below:
 
String x = Op.CustomField1;
x=x.replaceAll(',', ';');

Static method returns a string which you need to assign it back to your variable. 


Hope it helps.,

Cheers, 


Thanks,
balaji

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
Use it like below:
 
String x = Op.CustomField1;
x=x.replaceAll(',', ';');

Static method returns a string which you need to assign it back to your variable. 


Hope it helps.,

Cheers, 


Thanks,
balaji
This was selected as the best answer
Balaji Chowdary GarapatiBalaji Chowdary Garapati
A small correct in the above statement:

replaceAll is a Static string method returns a string which you need to assign it back to your variable.

missed to type replaceAll which brings a wrong impression to the statement

Cheers.,
 
Raj R.Raj R.
Thanks Balaji. That was a simple mistake.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
I know :) I made it once too., and it took me more than 5 hrs of head breaking debugging for that LOL :).

Cheers,
Raj R.Raj R.
Clearly I forgot to read the column that says Return Type. Yeah I initially thought it would just replace all of them without having to assign it back to the variable. I guess its similar to String.toUpperCase(). i should've known better. Thanks for the quick response!

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm