# How to uniformly get part of string elements from array of strings

**URL:** https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776
**Category:** General Usage
**Tags:** indexing, array
**Created:** [December 13, 2020, 9:23pm UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776 "2020-12-13T21:23:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Emmanouil\_Bakirtzis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanouil_bakirtzis/32/11456_2.png) [@Emmanouil\_Bakirtzis](https://discourse.julialang.org/u/Emmanouil_Bakirtzis)
#### Post date: [December 13, 2020, 9:23pm UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776/1 "2020-12-13T21:23:18Z")

</div>

Suppose that we have the array

```julia
z = ["aaa","bbb","ccc"]

```

and we want to get a new array where each element has only the first two characters

```julia
z_target = ["aa","bb","cc"]

```

I tried

```julia
getindex.(z,1:2)

```

but it thows the error  
ERROR: DimensionMismatch(“arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2”)

Any ideas?

---

<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: [December 13, 2020, 9:30pm UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776/2 "2020-12-13T21:30:34Z")

</div>

`getindex.(z,Ref(1:2))` will work — you need to tell it not to broadcast over the `1:2` array.

---

<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: [December 14, 2020, 1:38am UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776/3 "2020-12-14T01:38:05Z")

</div>

@Emmanouil_Bakirtzis, what about `chop.(z)` ?  
Or more generally: `chop.(z, head=0, tail=1)`, or better: `SubString.(z, 1, 2)`

---

<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: [December 14, 2020, 5:55pm UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776/4 "2020-12-14T17:55:08Z")

</div>

or, more literally,

```julia
julia> z = ["aaa","bbb","ccc"]
3-element Array{String,1}:
 "aaa"
 "bbb"
 "ccc"

julia> x = [s[1:2] for s in z ]
3-element Array{String,1}:
 "aa"
 "bb"
 "cc"

```

---

<div class="post-metadata">

### Author: ![Emmanouil\_Bakirtzis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanouil_bakirtzis/32/11456_2.png) [@Emmanouil\_Bakirtzis](https://discourse.julialang.org/u/Emmanouil_Bakirtzis)
#### Post date: [December 16, 2020, 8:24pm UTC](https://discourse.julialang.org/t/how-to-uniformly-get-part-of-string-elements-from-array-of-strings/51776/5 "2020-12-16T20:24:16Z")

</div>

Thanks, works perfect.
