# Replace $n$-th occurrence with number $n$

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} ?

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}  *)

• Wow, such a noble solution! ++ Apr 10, 2022 at 22:52
• An application of this method : Fruits = {"Apple", "Banana", "Orange", "Grapes"}; Module[{cnt = 0}, StringReplace["amkwajefiwaeah", "a" :> Fruits[[++cnt]]]] Then we get "ApplemkwBananajefiwOrangeeGrapesh" Apr 10, 2022 at 23:14
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"

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


1mkw2jefiw3e4h

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


A1mkwA2jefiwA3eA4h

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*)

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"

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} *)


A very basic approach:

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


yields: "1mkw2jefiw3e4h"