K Emery
K Emery

Reputation: 101

Is there a way to replace a character based off it's unicode code?

I currently have a string which contains multiple characters. Certain characters need replacing with others based on their Unicode. For example '£' needs to become GBP. I have a Dictionary of the conversions like so.

Key = u00a3 Value = GBP

Currently I have this code but it trips up when it comes across characters that don't need any conversion (or aren't in the dictionary), for example an empty space remains as u0020.

var unicodeStringList = str.Select(t => $"u{Convert.ToUInt16(t):X4} ".ToLower()).ToList();
            str = string.Join("", unicodeStringList);

            foreach (var (key, value ) in characterSet)
            {
                str = str.Replace(key, value );
            }
            return str;

Is there any way I can just replace the characters without having to do any weird conversions, for example something that works like a string.replace?

    str = "£££";

    foreach (var (key, value ) in characterSet)
            {
                str = str.Replace(key, value );
            }/// expected output to be "GBPGBPGBP"

Upvotes: 1

Views: 1011

Answers (2)

K Emery
K Emery

Reputation: 101

Ended up solving it like this:

            foreach (var (key, value ) in characterSet)
            {
                var hexValue = int.Parse(key.Substring(1), System.Globalization.NumberStyles.HexNumber);
                str = str.Replace(((char)hexValue).ToString(), value );
            }
            return str;

Upvotes: 1

Johnathan Barclay
Johnathan Barclay

Reputation: 20353

Is there any way I can just replace the characters without having to do any weird conversions?

Not out of the box, but you can define one:

static string ToUnicode(this char c) => $"u{Convert.ToUInt16(t):X4}";

Then you just need to return the converted string if it is in the dictionary:

var replacedCharacters = str
    .Select(original => characterSet.TryGetValue(original.ToUnicode(), out var replaced)
        ? replaced
        : original.ToString());

And then concatenate them together:

str = string.Concat(replacedCharacters);

Upvotes: 2

Related Questions