ToUpperCases based on the original string

Suppose I have a numberic string

str = "1234567456";


and another string called

nstr = "05eccead24";


I want to transform the non-digit character in nstr, that number corresponding to the same position in str is even, to upper cases. For example, the first non-digit character in nstr is e, and the position is 3, the same postion in str is 3 which is odd, thus we keep it in lowercase. While for the next char c in nstr, the position is 4 and the corresponding character in str is 4, which is even, so we should change it into uppercase.

The expected output should be:

"05eCcEaD24"


This works:

MapThread[If[EvenQ[#2], ToUpperCase[#1], #1] &, {Characters @ "05eccead24",
IntegerDigits @ FromDigits @ "1234567456"}] // StringJoin


• Why community wiki? Should I have added my method rather than posting separately? – Mr.Wizard Jul 15 '16 at 2:36
• @Mr. Wizard, fret not, I've upvoted yours. I know mine will work, but I cannot test it at the moment (currently using the gedanken version). – J. M. will be back soon Jul 15 '16 at 2:38
• It seems your method is orders of magnitude faster than mine. If I cannot improve that I'll probably delete my answer. Incidentally a line cannot start with //. I hope you don't mind if I reformat your code. – Mr.Wizard Jul 15 '16 at 2:49
• I rewrote my code to avoid StringReplacePart and it is now ~2.9 times faster than this. – Mr.Wizard Jul 15 '16 at 3:15
• You do know that quite a number of my answers last year were done without my having access to Mathematica, no? ;) – J. M. will be back soon Jul 16 '16 at 14:45

Edit: I first used StringReplacePart but that function is slow when used for many replacements, much as MapAt is. I have rewritten my code using a method from Map a function across a list conditionally and it is now orders of magnitude faster on long strings.

f[source_String, target_String] :=
Module[{new = Characters @ target},
(new[[#]] = ToUpperCase @ new[[#]]) & @
StringPosition[source, Characters @ "02468"][[All, 1]];
new <> ""
]


"05eCcEaD24"

• Maybe DigitCharacter and EvenQ can replace that long stream of text,but I have no ideal – yode Jul 15 '16 at 2:49
• @yode Literal patterns are typically faster however. – Mr.Wizard Jul 15 '16 at 3:12
• It is very good answer, but to me (I will not concern the time consumming stuff) @J.M. 's answer is more clean (and easy to understand). – van abel Jul 15 '16 at 3:17
• @vanabel Thanks, and no problem; I like clean code too. :-) – Mr.Wizard Jul 15 '16 at 3:18
• @ShutaoTANG Certainly that is true in many areas, though there are also cases where Mathematica is competitively fast, usually when operating on packed arrays with calls to fast library (Intel MKL, etc.) functions. However why are you commenting this here? Edit: Oh, I see you wrote a fast answer. +1 on that. I wonder if I can improve on my pure-Mathematica method. – Mr.Wizard Jul 17 '16 at 3:10

Should be quick for large strings if you want to keep it native MMA:

upem[s_, t_] :=
Module[{ss = Pick[Range@StringLength@s, EvenQ@ToCharacterCode[s]],
tc = ToCharacterCode@t},
tc[[Intersection[ss, Pick[Range@StringLength@t, Unitize@Clip[tc, {97, 122}, {0, 0}],1]]]] -= 32;
FromCharacterCode@tc];


E.g.

upem["1234567456", "05eccead24"]