12
$\begingroup$

How can I replace $n$-th occurrence with number n for string/for list?

For string :

StringReplace["amkwajefiwaeah", "a" -> "A"]
"AmkwAjefiwAeAh"

But how can I get

"1mkw2jefiw3e4h" or
"A1mkwA2jefiwA3eA4h" ?

For list :

ReplaceAll[{a,m,k,w,a,j,e,f,i,w,a,e,a,h},a->A]
{A,m,k,w,A,j,e,f,i,w,A,e,A,h}

But how can I get

{1,m,k,w,2,j,e,f,i,w,3,e,4,h} or
{{A,1},m,k,w,{A,2},j,e,f,i,w,{A,3},e,{A,4},h} ?

$\endgroup$

7 Answers 7

14
$\begingroup$

Perhaps this?:

Module[{cnt = 0}, 
 StringReplace["amkwajefiwaeah", "a" :> "A" <> ToString[++cnt]]]

(*  "A1mkwA2jefiwA3eA4h"  *)

And this?:

Module[{cnt = 0}, 
 ReplaceAll[{a, m, k, w, a, j, e, f, i, w, a, e, a, h}, 
  a :> {A, ++cnt}]]

(*  {{A, 1}, m, k, w, {A, 2}, j, e, f, i, w, {A, 3}, e, {A, 4}, h}  *)
$\endgroup$
2
  • $\begingroup$ Wow, such a noble solution! ++ $\endgroup$
    – imida k
    Apr 10, 2022 at 22:52
  • $\begingroup$ An application of this method : Fruits = {"Apple", "Banana", "Orange", "Grapes"}; Module[{cnt = 0}, StringReplace["amkwajefiwaeah", "a" :> Fruits[[++cnt]]]] Then we get "ApplemkwBananajefiwOrangeeGrapesh" $\endgroup$
    – imida k
    Apr 10, 2022 at 23:14
8
$\begingroup$
str = "amkwajefiwaeah"
pos = StringPosition[str, "a"]

{{1, 1}, {5, 5}, {11, 11}, {13, 13}}

StringReplacePart[str, ToString /@ (Range[Length@pos]), pos]

"1mkw2jefiw3e4h"

StringReplacePart[str, 
 "A" <> # & /@ ToString /@ (Range[Length@pos]), pos]

"A1mkwA2jefiwA3eA4h"


For the example you mentioned:

fruits = {"Apple", "Banana", "Orange", "Grapes"};

StringReplacePart[str, fruits, pos]

"ApplemkwBananajefiwOrangeeGrapesh"

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for introducing StringReplacePart ! $\endgroup$
    – imida k
    Apr 11, 2022 at 9:16
4
$\begingroup$
cnt=1;
"amkwajefiwaeah" //
StringSplit[#,""]& //
Map[If[# == "a", ToString[cnt++], #]&] // 
StringJoin

1mkw2jefiw3e4h

cnt=1;
"amkwajefiwaeah" // 
StringSplit[#,""]& // 
Map[If[# == "a", "A" <> ToString[cnt++], #]&] // 
StringJoin

A1mkwA2jefiwA3eA4h

$\endgroup$
4
$\begingroup$
string = "amkwajefiwaeah";

list = Characters@string;

c = "a";

Get positions using PositionIndex:

p = List /@ Lookup[PositionIndex[list], c]

{{1}, {5}, {11}, {13}}

The replacements:

rep = "" <> # & /@ 
      ToUpperCase@
      MapAt[ToString@#[[1]] &, 
      MapIndexed[List, Array["a" &, Length@p], {-1}], {All, 2}]

{"A1", "A2", "A3", "A4"}

Finally, using ReplacePart:

StringJoin @ ReplacePart[list, Thread[p -> rep]] === string

(*True*)
$\endgroup$
2
$\begingroup$
string = "amkwajefiwaeah";

list = Characters @ string;

c = "a";

Get positions

p = Flatten @ Position[list, c]

{1, 5, 11, 13}

For list

a = SubsetMap[(ToString /@ Range @ Length @ p) &, list, p]

{"1", "m", "k", "w", "2", "j", "e", "f", "i", "w", "3", "e", "4", "h"}

 b = ReplacePart[list, ReplacePart[p, i_ :> p[[i]] -> {ToUpperCase @ c, ToString @ i}]]

{{"A", "1"}, "m", "k", "w", {"A", "2"}, "j", "e", "f", "i", "w", {"A", "3"}, "e", {"A", "4"}, "h"}

For string

StringJoin @@ a

"1mkw2jefiw3e4h"

StringJoin @@ b

"A1mkwA2jefiwA3eA4h"

$\endgroup$
2
$\begingroup$

For list:

Function[{lst, el}, MapIndexed[Replace[#, el -> Count[lst[[;; #2[[1]]]], el]] &, 
    lst]][{a, m, k, w, a, j, e, f, i, w, a, e, a, h}, a]

(* {1, m, k, w, 2, j, e, f, i, w, 3, e, 4, h} *)

Or

Function[{lst, prefix, el}, MapIndexed[Replace[#, el -> {prefix,
   Count[lst[[;; #2[[1]]]], el]}] &, lst]][{a,m,k,w,a,j,e,f,i,w,a,e,a, h}, A, a]


(* {{A, 1}, m, k, w, {A, 2}, j, e, f, i, w, {A, 3}, e, {A, 4}, h} *)
$\endgroup$
2
$\begingroup$

A very basic approach:

s = "amkwajefiwaeah";
e = Characters[s];
c = 0;
StringJoin[If[# == "a", c++; ToString[c], #] & /@ e]

yields: "1mkw2jefiw3e4h"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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