BetaCodeShareBeta

by Code Solutions Project

Simple solutions for common problems

Description:Replacement of special characters in Spanish with RTF-compatible characters
Java
JoseluCross
public String toRTF(String text){
	String middle = text
    middel = middle.replace("á", "\\'e1");
    middel = middel.replace("Á", "\\'c1");
    middel = middel.replace("é", "\\'e9");
    middel = middel.replace("É", "\\'c9");
    middel = middel.replace("í", "\\'ed");
    middel = middel.replace("Í", "\\'cd");
    middel = middel.replace("ó", "\\'f3");
    middel = middel.replace("Ó", "\\'d3");
    middel = middel.replace("ú", "\\'fa");
    middel = middel.replace("Ú", "\\'da");
    middel = middel.replace("ü", "\\'fc");
    middel = middel.replace("Ü", "\\'dc");
    middel = middel.replace("ñ", "\\'f1");
    middel = middel.replace("Ñ", "\\'d1");
    return middel;
}

Input example

España
Quién lo habría dicho
Él ladrón entró rápidamente

Output example

Espa'f1a
Qui'e9n lo habr'eda dicho
'c9l ladr'f3n entr'f3 r'e1pidamente

Other implementations

C#
private string sustituir(string rtf){
    string middel;
    middel = rtf.Replace("á", "\\'e1");
    middel = middel.Replace("Á", "\\'c1");
    middel = middel.Replace("é", "\\'e9");
    middel = middel.Replace("É", "\\'c9");
    middel = middel.Replace("í", "\\'ed");
    middel = middel.Replace("Í", "\\'cd");
    middel = middel.Replace("ó", "\\'f3");
    middel = middel.Replace("Ó", "\\'d3");
    middel = middel.Replace("ú", "\\'fa");
    middel = middel.Replace("Ú", "\\'da");
    middel = middel.Replace("ü", "\\'fc");
    middel = middel.Replace("Ü", "\\'dc");
    middel = middel.Replace("ñ", "\\'f1");
    middel = middel.Replace("Ñ", "\\'d1");
    return middel;
}
Python
def toRTF(text):
    middle = text
    middel = middle.replace("á", "\\'e1")
    middel = middel.replace("Á", "\\'c1")
    middel = middel.replace("é", "\\'e9")
    middel = middel.replace("É", "\\'c9")
    middel = middel.replace("í", "\\'ed")
    middel = middel.replace("Í", "\\'cd")
    middel = middel.replace("ó", "\\'f3")
    middel = middel.replace("Ó", "\\'d3")
    middel = middel.replace("ú", "\\'fa")
    middel = middel.replace("Ú", "\\'da")
    middel = middel.replace("ü", "\\'fc")
    middel = middel.replace("Ü", "\\'dc")
    middel = middel.replace("ñ", "\\'f1")
    middel = middel.replace("Ñ", "\\'d1")
    return middel
×Oh snap! Something wrong