0

I have a requirement to translate few predetermined spanish characters to english in field values of different custom objects. I have tried to create a generic code. can someone please let me know where it fails to give desired output?

Apex class snippet:

public static String translate(String str)
{
  M= new Map<String,String>{'á'=>'a', 'é'=>'e' ,'í'=>'i', 'ó'=>'o', 'ú'=>'u', 'ü'=>'u', 'ñ'=>'n'};

  for (String key: M.keyset())//iterate over keyset
  {
    str.replace(key,M.get(key));//Get CORR Value
  }

  return str;//english
}
  • What error do you get, if any? – Koen Wesselman Jan 30 '15 at 11:19
  • I am not getting any error in debug log but simply this desired translation I am not getting – Testing_SFDC Jan 30 '15 at 11:22
2

The replace() function does not edit the variable you put in front of it but returns a new String. Please replace:

str.replace(key,M.get(key));//Get CORR Value

with:

str = str.replace(key,M.get(key));//Get CORR Value
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.