# Substitute the value of a variable intro the string

**URL:** <https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [January 12, 2020, 12:23am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254 "2020-01-12T00:23:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [January 12, 2020, 12:23am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254/1 "2020-01-12T00:23:06Z")

</div>

I have a variable: X  
I have a text: " … $X…" from a text file.

What I can do, to substitute the value of variable X into the text?  
I know how it can be done directly in the code: text = " bla bla bla $x … ", But if the text is been reding from the file.txt. How I can do it?

> text = read(“test.txt”, String)  
> X = 5;  
> do something for substitule value of X into the text  
> println(text)

Thank you in advance

---

<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:** [January 12, 2020, 12:28am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254/2 "2020-01-12T00:28:47Z")

</div>

```julia
julia> text = "blah blah \$X blah"
"blah blah \$X blah"

julia> X = 5
5

julia> replace(text, "\$X" => "$X")
"blah blah 5 blah"

```

---

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [January 12, 2020, 12:32am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254/3 "2020-01-12T00:32:12Z")

</div>

Thank you.

If I have many variables? Is there other way, to do it one command?

---

<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:** [January 12, 2020, 12:49am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254/4 "2020-01-12T00:49:02Z")

</div>

do you mean this, [Replacing multiple strings errors - #4 by bkamins](https://discourse.julialang.org/t/replacing-multiple-strings-errors/13654/4)

---

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [January 12, 2020, 2:13am UTC](https://discourse.julialang.org/t/substitute-the-value-of-a-variable-intro-the-string/33254/5 "2020-01-12T02:13:26Z")

</div>

may be.  
I just want to execute all $command in already existing string.
