4
$\begingroup$

So I have the following string:

y={"110001011111011001000010", "00001010001000100111", \
"11000011011100111110", "110011101001010110110", \
"1001010011100001010001000", "01111111001100010111", \
"10010111111001110111111", "01111100001011001100", \
"00101111000111111111101", "0001011011011101010100", \
"01010110110110000010", "110011111010011100111", \
"11101101001100101100", "10110010101100000000", \
"0010111100011011110111111", "0001011011011001001100", \
"00111100001100111110", "10111111101101010110110", \
"10110011111101010110110", "00111011001000101010", \
"0100111110001110111100", "110101101100001110000001", \
"011000100011001100001010001000", "000000101001100010111", \
"11111101011100111111", "10101111001111000010", \
"110111001101110000001000110011", "000000110101100010100", \
"01010111100001011011011", "11001110110001011110001", \
"10001100100000101101100", "000111000101111101000000", \
"1000101011000001010001000", "01010110101100010111", \
"1100110010010101001011011", "00001010101000010000", \
"010000011100001010001000", "1010111010101100010111", \
"100101001110001110001011", "01111111001000110010"}

What I am trying to is add a "0." at the start of every element. I thought about using a For loop to do this. But is there another way?

$\endgroup$
3
  • $\begingroup$ Which String function, if any, have you tried so far? $\endgroup$
    – Syed
    Apr 7 at 15:05
  • $\begingroup$ I am trying to use StringReplace and StringJoin, but i am not sure if it is appropiate. I did this For[i = 1, i <= Length[y], i++, y[[i]] = StringReplace[y[[i]], y[[i]] -> StringJoin["0.", y[[i]]]]] but I am not sure if there a more efficient way $\endgroup$ Apr 7 at 15:11
  • 4
    $\begingroup$ "0" <> # & /@ y $\endgroup$
    – Rupesh
    Apr 7 at 15:23

2 Answers 2

6
$\begingroup$

With your y as defined above:

StringJoin["0", #] & /@ y

adds a zero to the front of each term.

$\endgroup$
1
  • 2
    $\begingroup$ Or the abbreviated form "0" <> # & /@ y $\endgroup$
    – Bob Hanlon
    Apr 7 at 16:45
3
$\begingroup$

With y taken from the OP

StringInsert[y, "0", 1]

string

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.