Special euro characters

231 views Asked by At

I have a string that seems to contain a special character :

my_str                                   # [1] "0€ de frais de courtage"
my_str == "0€ de frais de courtage"      # [1] FALSE
gsub("€","X",my_str)                     # [1] "0€ de frais de courtage"
gsub("€","X","0€ de frais de courtage")  # [1] "0X de frais de courtage"

I would like to replace it by the standard character i.e. the one I can type with Alt-gr + E.

These strings come from file names I got with list.files

How can I do this ?

EDIT:

utf8ToInt(my_str)
# [1]   48 8364   32  100  101   32  102  114   97  105  115   32  100  101   32   99  111  117  114  116   97  103  101
utf8ToInt(stringi::stri_enc_toutf8("0€ de frais de courtage"))
# [1]  48 128  32 100 101  32 102 114  97 105 115  32 100 101  32  99 111 117 114 116  97 103 101
1

There are 1 answers

0
moodymudskipper On

The strings appear identical but the character is different.

In my EDIT at the bottom of the post we see that the code for the regular is 128 while the code for the troublesome is 8364.

and sure enough:

gsub(intToUtf8(8364),"X",my_str)  # [1] "0X de frais de courtage"

So the solution I was looking for is:

gsub(intToUtf8(8364),"€",my_str)

It replaces the "wrong" character with a "regular" 128 azerty alt gr + E symbol.