Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  10] [ 1]  / answers: 1 / hits: 94156  / 8 Years ago, wed, february 3, 2016, 12:00:00

I have a string in unicode that i need to convert. I need to render the string with u00f3 to ó. This is an example, it should happen with all other types of characters, á, í, ú...



I have the following basic code:
https://jsfiddle.net/dddf7o70/



I need to convert



<Hello name=Informaciu00f3n />


into



Información

More From » reactjs

 Answers
22

If you have to work with strings that have, for whatever reason, literally the letters u followed by hexadecimals in them instead of being real letters, convert them to numbers, and then use String.fromCharCode() to turn those numbers into real letters. We can use a regexp replace with handler function for this:


// put this in whatever utility library file you have:
export function convertUnicode(input) {
return input.replace(/\+u([0-9a-fA-F]{4})/g, (a,b) =>
String.fromCharCode(parseInt(b, 16)));
}

And then in your component:


import { convertUnicode } from "...your util lib...";

function Hello(props) {
const name = convertUnicode(props.name);
return <div>Hello {name}</div>;
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Hello name="Informaci\u00f3n"/>);

Fiddle: https://jsfiddle.net/shdye3pq/


[#63468] Sunday, January 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradford

Total Points: 709
Total Questions: 117
Total Answers: 91

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago
;