# How to clean strings?

**URL:** <https://discourse.julialang.org/t/how-to-clean-strings/45917>\
**Category:** General Usage\
**Tags:** vscode\
**Created:** [September 1, 2020, 7:08pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917 "2020-09-01T19:08:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [September 1, 2020, 7:08pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/1 "2020-09-01T19:08:16Z")

</div>

I have 2 strings that as a human appear exactly the same, but Julia sees 2 different strings.

```julia
test1 = raw"C:\Program Files\MATLAB\R2019a"
test2 = raw"C:\Program Files\MATLAB\R2019a"

test1 == test2 #julia says this is false

```

It turns out that Julia sees the space as a different character:

```julia
julia> test1[11]
' ': Unicode U+00A0 (category Zs: Separator, space)

julia> test2[11]
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)

```

The reason why this is a problem for me is the following (Julia doesn’t think `test1` is a valid path):

```julia
julia> isdir(test1)
false

julia> isdir(test2)
true

```

Not sure how I ended up with this different space character, I think it’s from coping and pasting from OneNote. What is a good practice to avoid this problem in the future? Maybe a setting in VS Code so I can “see” the space character as different. Maybe a `clean` function to convert all chars to ASCII/Unicode?

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 1, 2020, 7:38pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/2 "2020-09-01T19:38:37Z")

</div>

The first is a no break space. Maybe you could use the `isspace` function to replace all spaces with `' '` (0x20).

Edit  
Or of course regex

See [Strings · The Julia Language](https://docs.julialang.org/en/v1/base/strings/#Base.replace-Tuple%7BAbstractString,Pair%7D)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 1, 2020, 8:33pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/3 "2020-09-01T20:33:06Z")

</div>

Hmmm… maybe the question from Brad\_Carman was more general… it happened to me as well sometimes that I had problems when a string had characters almost invisible or rendered in the same way, for example “-” has an other very similar character…

But I don’t know how to solve this… a “sanitise” function?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 1, 2020, 8:52pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/4 "2020-09-01T20:52:40Z")

</div>

you can use a `regex` with `replace` to clean out any white space character to some unified space character maybe.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 1, 2020, 11:20pm UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/6 "2020-09-01T23:20:24Z")

</div>

> [@sylvaticus](#):
>
> But I don’t know how to solve this… a “sanitise” function?

You could use NFKC normalization, which removes some confusable characters but not necessarily all. It works in this case because a non-breaking space normalizes to an ordinary space:

```julia
julia> import Unicode

julia> Unicode.normalize(test1, :NFKC) == Unicode.normalize(test2, :NFKC)

```

However, be aware that NFKC normalization will also treat some characters as equivalent even though they are visually distinct:

```julia
julia> Unicode.normalize("𝐴ᶜ𝐇𝕆𝓞", :NFKC)
"AcHOO"

```

There is also NFC normalization, which will only treat characters as the same if they are visually and semantically identical — typically this is to canonicalize combining characters.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 2, 2020, 12:32am UTC](https://discourse.julialang.org/t/how-to-clean-strings/45917/7 "2020-09-02T00:32:27Z")

</div>

> [@danielw2904](#):
>
> no break space.

😱
