In the string class apex, what does the .substring function do?

826    Asked by BuffyHeaton in Salesforce , Asked on Apr 17, 2023

See I'm getting this field from an integration with wookie, and the field should go to Salesforce in currency format. I get it with the following value: BRL 20.000,00 (Twenty thousand reais)I need to treat this value, removing the letters, removing the "." and replacing the "," with "." The final result should be a decimal 20000.00 So far I'm:

//With this line I get the result I want
acc.Capital_Social__c= Decimal.valueOf((String.valueOf(results.get('capital_social')).replaceAll('[^0-9,]', '')).replace(',', '.'));
The problem is that before I implemented this solution, this was the code used:
acc.Capital_Social__c = Decimal.valueOf(String.valueOf(results.get('capital_social')).substring(3).remove('.').replace(',', '.'));
You will notice that I stopped using the function

.substring(3)

acc.Capital_Social__c= Decimal.valueOf((String.valueOf(results.get('capital_social')).replaceAll('[^0-9,.]', '')).substring(3).remove('. ').replace(',', '.'));

Because when I put it the result generated is: 0.00

I don't understand why I get this result. I don't understand what this ".substring(3)" function was doing in the old code, on google I didn't enter an explanation about this function. I'm afraid that she will be missed at some point. I would like to know if anyone knows this function, if they understand its purpose, if it is really needed, and why it is returning 0.00

Answered by David EDWARDS

From the String class apex documentation-


substring(startIndex)

Returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String. So given that String.valueOf(results.get('capital_social')) evaluates to BRL 20.000,00 (Twenty thousand reais)... following that with substring(3) was previously how you were removing the "BRL" (but not the space between that and the numeric portion) from the beginning of your string. substring(3) cuts out the first 3 characters (the returned string starts with character 4 (i.e. index 3) of the original string)

 B  R  L     2  0  .  0  0  0  ,   0   0  ...
[0][1][2][3][4][5][6][7][8][9][10][11][12]...
[discard][ keep ]
When you changed the code to use .replaceAll('[^0-9,.]', '') though, that was taking care of removing the "BRL " (with the space) as well as the "(Twenty thousand reais)", leaving you with 20,000.00
By keeping the substring(3), you were now discarding part of your desired data
 2 0 . 0 0 0 , 0 0
[0][1][2][3][4][5][6][7][8]
[discard][ keep ]
Now your string is 000,00, and you remove "." (which doesn't exist in your string at this point) and replace(',', '.').
So the final step in that statement is effectively Decimal.valueOf('000.00').

Leading zeros are removed, except for the first digit, leaving you with your observed value of 0.00.

By removing the substring(3), the rest of your steps were working on the value 20.000,00. With no data discarded, the rest of your processing steps ended up as Decimal.valueOf('20000.00');



Your Answer

Interviews

Parent Categories