# String questions

**URL:** <https://discourse.julialang.org/t/string-questions/89375>\
**Category:** New to Julia\
**Tags:** question, metaprogramming\
**Created:** [October 27, 2022, 2:27pm UTC](https://discourse.julialang.org/t/string-questions/89375 "2022-10-27T14:27:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)\
**Post date:** [October 27, 2022, 2:27pm UTC](https://discourse.julialang.org/t/string-questions/89375/1 "2022-10-27T14:27:50Z")

</div>

```julia
chr="ppp"
pppfile="c:/Users/Dsektop/"*chr*".csv"

```

I want to use chr to generate the “pppfile”.I tried like the following

```julia
julia> ($chr)file="c:/Users/Dsektop/"*chr*".csv"
ERROR: syntax: extra token "file" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

But it failed.How do I use chr to generate pppfile(just the name)?Thanks

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2022, 2:33pm UTC](https://discourse.julialang.org/t/string-questions/89375/2 "2022-10-27T14:33:26Z")

</div>

You would need to use `eval`, but that is probably not the right path. If you have many files for which you need to associate names, how about using a dict:

```julia
julia> files = Dict{String,String}()
Dict{String, String}()

julia> files[chr*"file"] = "c:/Users/"*chr*".csv"
"c:/Users/ppp.csv"

julia> files["pppfile"]
"c:/Users/ppp.csv"

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 27, 2022, 4:17pm UTC](https://discourse.julialang.org/t/string-questions/89375/3 "2022-10-27T16:17:29Z")

</div>

Using `eval` here seems to require a lot of string escaping:

```julia
file = "c:/Users/Desktop/" * chr * ".csv"
eval(Meta.parse("$(chr)file = \"\$(file)\""))

```

Warning: using `eval` is not recommended by Julia’s experts and alternatives like @lmiq’s nice solution are preferable.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 27, 2022, 4:49pm UTC](https://discourse.julialang.org/t/string-questions/89375/4 "2022-10-27T16:49:47Z")

</div>

> [@zhangchunyong](#):
>
> `($chr)file="c:/Users/Dsektop/"*chr*".csv"`

The simple answer is “Don’t do this”. Dynamically generating variable names is a bad idea, and you should instead use a collection datatype, probably `Dict`, as @lmiq suggested.

There are also better and safer ways of building the file path. For example, you can use `normpath`, which fixes your path to be correct for the current platform:

```julia
julia> normpath("c:/Users/Desktop", chr*".csv")
"c:\\Users\\Desktop\\ppp.csv"

```

It sets the correct path separator, and also removes `..` etc:

```julia
julia> normpath("c:/Users/../Desktop", chr*".csv")
"c:\\Desktop\\ppp.csv"

1.8.0> normpath("C:/", "Users", "Desktop", chr*".csv")
"C:\\Users\\Desktop\\ppp.csv"

```
