WolframLang: String Replace

By Xah Lee. Date: . Last updated: .

Replace by Position Range

StringReplacePart

replace string by position range.

StringReplacePart

StringReplacePart[ "abcdef", "xx", {1,4}]
(* "xxef" *)

Replace by String Pattern

StringReplace[ str , findStr -> replaceStr]

replace string.

the findStr can be a pattern, represented by Regular Expression or String Expression

StringReplace

StringReplace[ "some dogs" , "dog" -> "cat"]
(* "some cats" *)
(* replace letter prefixed digits by x prefix *)

StringReplace[ "good g935 m66 z024 m12 g904 q08",
 RegularExpression["[a-z](\\d+)"] -> "x$1"
]

(* good x935 x66 x024 x12 x904 x08 *)
StringReplace[ str , listOfPairs]

Replace multiple pairs of strings. Replacement does not depend on previous replacement.

(* replace multiple pairs of strings. replacement does not depend on previous replacement. *)

StringReplace["abc", {"a"->"x", "b"->"y", "x"->"H"}]
(* xyc *)

WolframLang String