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
Makam RuthvikMakam Ruthvik 

How to replace two characters of a String at the same time

Decimal d1 = 491345345.90;
        System.debug(d1);
        List<String> args = new String[]{'0','number','###,###,##0.00'};
String s = String.format(d1.format(), args);
System.debug(s);

Using this I was able to format the decimal.

Now I have to change to Netherlands currently i.e., replacing all the dots with commas and commas with dots.

How to change the string from 491,345,345.90 to 491.345.345,90 in one go

Best Answer chosen by Makam Ruthvik
ravi soniravi soni
hy Makam,
try following code.
string w = '491,345,345,90';//Output =>  491.345.345.90
string x = '491,345,345.90';//OUtput =>  491.345.345,90
string y = '491.345.345,90';//Output =>  491,345,345.90
string z = '491.345.345.90';//Output => 491,345,345,90

string newString = '';
list<string> lstString = new list<string>();
list<string> splitData = y.split(',');
for(integer i=0; i<splitData.size(); i++){
    if(splitData[i].contains('.')){
         lstString.add(splitData[i].replace('.',','));
    }
    else {
        lstString.add(splitData[i]);
    }
}
newString = string.join(lstString,'.');
system.debug('newString : ' + newString);
there was 4 conditions and we Handled it properly.if you want to check your answer then simply you have to replace y before split.
For example => 1.   x.split(',');
2.   y.split(',');
3.   z.split(',');
4.   w.split(',');
let me know if it helps you and don't forget to mark it as best answer.
Thank you

All Answers

ravi soniravi soni
hy Makam,
try following code.
string w = '491,345,345,90';//Output =>  491.345.345.90
string x = '491,345,345.90';//OUtput =>  491.345.345,90
string y = '491.345.345,90';//Output =>  491,345,345.90
string z = '491.345.345.90';//Output => 491,345,345,90

string newString = '';
list<string> lstString = new list<string>();
list<string> splitData = y.split(',');
for(integer i=0; i<splitData.size(); i++){
    if(splitData[i].contains('.')){
         lstString.add(splitData[i].replace('.',','));
    }
    else {
        lstString.add(splitData[i]);
    }
}
newString = string.join(lstString,'.');
system.debug('newString : ' + newString);
there was 4 conditions and we Handled it properly.if you want to check your answer then simply you have to replace y before split.
For example => 1.   x.split(',');
2.   y.split(',');
3.   z.split(',');
4.   w.split(',');
let me know if it helps you and don't forget to mark it as best answer.
Thank you
This was selected as the best answer
Makam RuthvikMakam Ruthvik

HI veer soni

I got a very simple fix for it rather than going loops. But anyways thanks for giving me another approach.

Just for you information the below works just fine

OrderCashbackVal = OrderCashbackVal.replace(',', '@');
                OrderCashbackVal = OrderCashbackVal.replace('.', ',');
                OrderCashbackVal = OrderCashbackVal.replace('@', '.');