# string to complex number

Consider the following list

MyList= {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"}

Head /@ MyList (* -> {Real, Real, String} *)


In fact, the String is a complex number.

Is there an easy way of converting MyList to MyNewList such that

Head /@ MyNewList (* -> {Real, Real, Complex} *)

-

myList = {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"} ;

myNewList = myList/.{x_, y_, z_} :> {x, y, ToExpression@StringReplace[z, "i" -> "I"]}


{-0.0000396, -0.0000187167, 0.102591 - 0.0677132 I}

Head /@ myNewList


{Real, Real, Complex}

-

For a general case, in which the string may be in any position, also this is possible:

MyList /. s_String :> ToExpression[ToUpperCase[s]]


Care should be taken if the numbers come from another program using the common scientific notation, such as 1.0E+7. In this case, the following may help:

myList /. s_String :> ToExpression[StringReplace[ToUpperCase[s], "E" -> "*^"]]

-

A little more general:

myList = {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"}
(ToExpression /@ myList) /. i -> I