# Detect string arrays in Matrix{Any} and convert them to number

**URL:** <https://discourse.julialang.org/t/detect-string-arrays-in-matrix-any-and-convert-them-to-number/104155>\
**Category:** General Usage\
**Tags:** strings, type\
**Created:** [September 22, 2023, 3:20pm UTC](https://discourse.julialang.org/t/detect-string-arrays-in-matrix-any-and-convert-them-to-number/104155 "2023-09-22T15:20:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)\
**Post date:** [September 22, 2023, 3:20pm UTC](https://discourse.julialang.org/t/detect-string-arrays-in-matrix-any-and-convert-them-to-number/104155/1 "2023-09-22T15:20:03Z")

</div>

Consider there is an m×n Matrix{Any}: where some of its arrays are decimal numbers (Float64) and some of them are strings of this type a.b.c, in which a, b, and c are integer numbers (a,b,c\>10). Now, how can we write a code in Julia to 1) find these strings; and 2) remove the second point and change them from string a.b.c to number a.bc?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 23, 2023, 3:12pm UTC](https://discourse.julialang.org/t/detect-string-arrays-in-matrix-any-and-convert-them-to-number/104155/2 "2023-09-23T15:12:28Z")

</div>

```julia
m = [11.23 "22.33.444"; "333.44.5555" 4.56]

function parse_matrix(m)
  map(parse_cell, m)
end

parse_cell(x :: Number) = x;

function parse_cell(x :: AbstractString)
	idx = findlast(c -> c == '.', x);
	return parse(Float64, x[1 : (idx-1)] * x[(idx+1) : end])
end

parse_matrix(m)

2×2 Matrix{Any}:
 11.23 "22.33.444"
   "333.44.5555" 4.56

```

I have a feeling that I made the string indexing clunky / slow. There is probably a more efficient way of deleting the 2nd period from a string (using regex, most likely).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 23, 2023, 4:54pm UTC](https://discourse.julialang.org/t/detect-string-arrays-in-matrix-any-and-convert-them-to-number/104155/3 "2023-09-23T16:54:56Z")

</div>

you could use one of these, but I don’t know if they are “better” than yours,

```julia
str="222.333.444"

reverse(replace(reverse(str),r"\." => "",count=1))

replace(str,r"\.(\d+)$" => s"\1")

```
