4

So i have a string for example like

"HITMAN\u2122 Free Trial"

is there any way i can convert the \u2122 to an actual unicode character so that the string would look like this

"HITMAN™ Free Trial"

Edit: for clarification the first string is an utf-8 string from an api, and i need to parse it for display

2
  • You mean, how to represent "HITMAN™ Free Trial" in source code using the \u escape or convert "HITMAN\\u2122 Free Trial" at runtime? Commented Jul 11, 2021 at 15:56
  • Convert at runtime to the unicode version Commented Jul 11, 2021 at 17:49

2 Answers 2

8

Just add {and} around 2122, like this:

let unicode_str = "HITMAN\u{2122} Free Trial";
println!("{}", unicode_str);

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry i was bit unclear, I added some clarification.
3

The first encoding is the one you find in many languages, and for example in JSON. It differs from Rust literals in which you have \u{2122} instead of \u2122.

This gives us a solution: parse it as JSON.


let s = "HITMAN\\u2122 Free Trial";
let s: String = serde_json::from_str(&format!("\"{}\"", s)).unwrap();
assert_eq!(
    s,
    "HITMAN™ Free Trial",
);

But while this is useful as validation, you probably don't want to include a deserializer just for this, so you probably want to do the parsing yourself, for example with a regular expression:

use lazy_regex::*;

let s = "HITMAN\\u2122 Free Trial";
let s = regex_replace_all!(r#"\\u(\d{4})"#, s, |_, num: &str| {
    let num: u32 = u32::from_str_radix(num, 16).unwrap();
    let c: char = std::char::from_u32(num).unwrap();
    c.to_string()
});
assert_eq!(
    s,
    "HITMAN™ Free Trial",
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.