# What is the library for arrays of strings?

**URL:** <https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164>\
**Category:** General Usage\
**Created:** [November 7, 2016, 2:40pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164 "2016-11-07T14:40:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 7, 2016, 2:40pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/1 "2016-11-07T14:40:17Z")

</div>

I often look in the docs to see what methods are available for arrays of strings, but it doesn’t look like there is a lot. Can anyone direct me to where a useful standard library for strings exist? `methodswith(Vector{String})` only gives a single result (`evalfile`), not even `join` as signature of join is `join(io::IO, strings, delim)` (strings is `Any`).

Here are some things I’d like to do:

1. Find all strings with a given substring.  
desired interface: something like `grep(strings::Vector{String}, pattern::Union{String, RegEx})`, i.e. `grep(strings, "word")`  
what I usually have to do instead: `find(_-> ! (search(_, pattern) == 0:-1), strings) #or filter`

2. Replace all instances of a pattern.  
desired interface: something like `gsub(strings, pattern, replacement)`  
what I usually have to do instead: `map(_->replace(_, pattern, replacement), strings)`

Thanks!

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [November 7, 2016, 2:58pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/2 "2016-11-07T14:58:35Z")

</div>

I don’t know of any libraries which do what you’re suggesting, but in 0.5 you can take advantage of generators to write for 1.

```julia
find(search(_, pattern) != 0:-1 for _ in strings)

```

For 2. you can use comprehensions,

```julia
[replace(_, pattern, replacement) for _ in strings]

```

or alternatively the `.` broadcast syntax

```julia
replace.(strings, [pattern], [replacement])

```

(It would be nice if we didn’t need the `[]` wrapper around the last 2 arguments though).  
-simon

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [November 7, 2016, 3:16pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/3 "2016-11-07T15:16:18Z")

</div>

Also, you can use `contains` instead of `search(...) != 0:-1`, though we really should probably combine that function with `ismatch` (see [#19250](https://github.com/JuliaLang/julia/issues/19250))

---

<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:** [November 7, 2016, 3:58pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/4 "2016-11-07T15:58:05Z")

</div>

You don’t need the `[...]` in 0.6 (see [broadcast should treat types as "scalar-like" · Issue #16966 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/16966)):

```julia
julia> replace.(["a234", "Foo", "!"], r"[a-z]", "_")
3-element Array{String,1}:
 "_234"
 "F__" 
 "!"   

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 7, 2016, 5:02pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/5 "2016-11-07T17:02:13Z")

</div>

nice, I did not know the trick with wrapping _the other_ arguments in an array to get the broadcast dot to work for strings (still great news that 0.6 will obviate this). The `replace.(...)` and the `contains.(strings, pattern)` syntaxes are perfect, and `[string for string in strings if contains(string,pattern)]` or `strings[contains.(strings, pattern)]` (for the `filter` version) is not too bad 🙂

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [August 30, 2018, 10:02am UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/6 "2018-08-30T10:02:21Z")

</div>

Hello,

I’m reviving this old topic because I’m trying to do the same as the OP in 0.7.

For the OP’s first question I’m using

```julia
somestrings = ["file1" , "hello" , "num123" , "foo_bar"]
filter(x->occursin(pattern,x),somestrings)

```

This works fine, but I’m wondering if there is a more ideomatic way in 0.7.  
I’ve seen that find and search are deprecated and replace was changed.

Many thanks,

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 30, 2018, 10:24am UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/7 "2018-08-30T10:24:18Z")

</div>

I think `filter` and a closure is perfectly idiomatic. If you want the indices, you can use `findall`.

The `find*` family of functions underwent an API redesign, you are [doing the right thing when using `v0.7` for upgrading your code](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321), after which you can switch to `v1.0`.

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [August 30, 2018, 12:07pm UTC](https://discourse.julialang.org/t/what-is-the-library-for-arrays-of-strings/164/8 "2018-08-30T12:07:41Z")

</div>

Thanks for the reply!
