# Substring replacement

**URL:** <https://discourse.julialang.org/t/substring-replacement/63794>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [June 30, 2021, 3:38am UTC](https://discourse.julialang.org/t/substring-replacement/63794 "2021-06-30T03:38:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpkwhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpkwhan/32/26456_2.png) [@dpkwhan](https://discourse.julialang.org/u/dpkwhan)\
**Post date:** [June 30, 2021, 3:38am UTC](https://discourse.julialang.org/t/substring-replacement/63794/1 "2021-06-30T03:38:13Z")

</div>

Why does the following not work?

```julia
replace("2021-06-29T23:14:20", "-" => "", ":" => "")

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 30, 2021, 4:09am UTC](https://discourse.julialang.org/t/substring-replacement/63794/2 "2021-06-30T04:09:54Z")

</div>

Because `replace` only takes one `pat => r` argument. Try putting them in a tuple.

---

<div class="post-metadata">

**Author:** ![dpkwhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpkwhan/32/26456_2.png) [@dpkwhan](https://discourse.julialang.org/u/dpkwhan)\
**Post date:** [June 30, 2021, 4:25am UTC](https://discourse.julialang.org/t/substring-replacement/63794/3 "2021-06-30T04:25:49Z")

</div>

In the official document, the following works:

```julia
replace([1, 2, 1, 3], 1=>0, 2=>4, count=2)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 30, 2021, 4:31am UTC](https://discourse.julialang.org/t/substring-replacement/63794/4 "2021-06-30T04:31:27Z")

</div>

That’s the method for an array. You can’t do that in a string (and it turns out, making a vector of substitution pairs won’t help):

> [@Perform multiple replacements on a string in a single pass](https://discourse.julialang.org/t/perform-multiple-replacements-on-a-string-in-a-single-pass/43247):
>
> I have a couple of scripts which perform multiple replacements on the same string. It seems as though this can be sped up by searching for all potential matches. For example, something like julia\>replace("123",[r"1" =\> s"a",r"2" =\> s"b"]) "ab3" To be clear, that isn’t real output. Or maybe something with an or logic. Note that putting a . after replace outputs an array with each individual substitution. ie julia\> replace.("123",[r"1" =\> s"a",r"2" =\> s"b"]) 2-element Array{String,1}: "a23" "…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 30, 2021, 5:10am UTC](https://discourse.julialang.org/t/substring-replacement/63794/5 "2021-06-30T05:10:43Z")

</div>

This will work in 1.7.

---

<div class="post-metadata">

**Author:** ![dpkwhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpkwhan/32/26456_2.png) [@dpkwhan](https://discourse.julialang.org/u/dpkwhan)\
**Post date:** [June 30, 2021, 10:09pm UTC](https://discourse.julialang.org/t/substring-replacement/63794/6 "2021-06-30T22:09:49Z")

</div>

Good to know this. Thank you, @Sukera

---

<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:** [June 30, 2021, 10:35pm UTC](https://discourse.julialang.org/t/substring-replacement/63794/7 "2021-06-30T22:35:31Z")

</div>

Converting string to array and back to string:

```julia
str = "2021-06-29T23:14:20"
join(replace(split(str,""), "-" => "", ":" => ""))

"20210629T231420"

```
