How to insert a character between any several characters without knowing indexes?
This is what you want ?
StringJoin[Riffle[Partition[Characters[#], 2], "&&"]] & /@ {"x1x2",
"x1", "x1x2x3", "x3", "x4", "x1x2x3x4"}
(* Out: {"x1&&x2", "x1", "x1&&x2&&x3", "x3", "x4", "x1&&x2&&x3&&x4"} *)
lst = {"x1x2", "x1", "x1x2x3", "x3", "x4", "x1x2x3x4"};
You can use a combination of StringRiffle
and StringPartition
StringRiffle[StringPartition[#, 2], "&&"] & /@ lst
{"x1&&x2", "x1", "x1&&x2&&x3", "x3", "x4", "x1&&x2&&x3&&x4"}
Alternatively, you can use StringReplace
:
StringReplace[lst, d : DigitCharacter .. ~~ a : LetterCharacter :> d <> "&&" <> a]
{"x1&&x2", "x1", "x1&&x2&&x3", "x3", "x4", "x1&&x2&&x3&&x4"}