StringReplace["string",ssp]
当 s 在 "string" 中出现时用 sp 将其替换.
StringReplace["string",{s1sp1,s2sp2,…}]
当 si 作为 "string" 的子串出现时,用 spi 将其替换.
StringReplace["string",srules,n]
仅执行前 n 个替换.
StringReplace[{s1,s2,…},srules]
给出每个 si 的结果列表.
StringReplace[srules]
表示 StringReplace 的运算符形式,可以将其应用于表达式.
StringReplace
StringReplace["string",ssp]
当 s 在 "string" 中出现时用 sp 将其替换.
StringReplace["string",{s1sp1,s2sp2,…}]
当 si 作为 "string" 的子串出现时,用 spi 将其替换.
StringReplace["string",srules,n]
仅执行前 n 个替换.
StringReplace[{s1,s2,…},srules]
给出每个 si 的结果列表.
StringReplace[srules]
表示 StringReplace 的运算符形式,可以将其应用于表达式.
更多信息和选项
- 字符串表达式 si 可以包含在 StringExpression 说明中指定的对象.
- StringReplace 搜索一个字符串,并从字符串的第一个位置开始逐个检测子串. 对于每个子串,它按次序进行指定的变换规则. 如果某个规则被可用,就用子串替换,然后在从该子串后的字符开始新一轮测试.
- 如果 si->spi 中的 spi 不能计算为字符串,StringReplace 将产生一个 StringExpression 而不是一个普通字符串.
- 在 si:>spi 形式的替换中,spi 直到每次使用时才进行计算.
- StringReplace 接受以下选项:
-
IgnoreCase False 匹配时是否忽略大小写 - 设置选项 IgnoreCase->True 使得 StringReplace 忽略大小写.
- StringReplace[srules][expr] 等价于 StringReplace[expr,srules].
- StringReplace[BioSequence["type","seq"],srules,…] 将根据产生生物分子序列的 srules 替换字符串 "seq" 中的出现. 在这种情况下,规则 si->spi 的 si 中的简并字母被解释为基于生物分子序列类型的通配符模式. 使用 Verbatim[si] 可从字面上匹配简并字母.
- BioSequence 的文档列出了每种类型的生物分子序列支持的简并字母.
- 如果 StringReplace 计算的生物分子序列是环状的,则可以进行环绕匹配.
范例
打开所有单元 关闭所有单元基本范例 (3)
StringReplace["abbaabbaa", "ab" -> "X"]StringReplace["ababbabbaaababa", "ab".. -> "X"]使用 StringReplace 的运算符形式:
StringReplace["ab".. -> "X"]["ababbabbaaababa"]范围 (12)
StringReplace["abc abcb abdc", "ab" ~~ _ -> "X"]StringReplace["abc abcd abcd", WordBoundary ~~ "abc" ~~ WordBoundary -> "XX"]StringReplace["abcd acbd", RegularExpression["[ab]"] -> "XX"]StringReplace["abc 31/12/2003 abcd 5/3/2004 abc", DatePattern[{"Day", "Month", "Year"}] -> "XX"]StringReplace["abcd acbd", RegularExpression["[ab]"] ~~ _ -> "YY"]为了计算规则左边,用 RuleDelayed (:>):
StringReplace["101abcd102abcd", a : DigitCharacter.. :> ToString[ToExpression[a] ^ 2]]StringReplace["abcddbbcbbbacbbaa", "bb" -> "X", 2]StringReplace["abcdabcdaabcabcd", {"abc" -> "Y", "d" -> "XXX"}]StringReplace 仅作用于特定字符:
StringReplace["product: A ⊕ B" , "⊕" -> "x"]StringReplace 自动线性作用于字符串列表:
StringReplace[{"aaabbbbaaaa", "bbbaaaab", "aaabab"}, "ab" -> "X"]StringReplace[BioSequence["DNA", "ATGGCGGT"], "GG" -> "TC"]StringReplace[BioSequence["DNA", "GTTGGCTTG"], "GGN" -> "A"]"N" 是简并字母,且除非在生物分子序列中的情况,否则也不是通配符:
StringReplace["GTTGGCTTG", "GGN" -> "A"]StringReplace[BioSequence["CircularDNA", "GTTGGCTTG"], "GGN" -> "A"]使用 Verbatim 仅替换字面上简并字母的出现:
StringReplace[BioSequence["DNA", "GTTGGCTTG"], "GG" ~~ Verbatim["N"] -> "A"]推广和延伸 (1)
选项 (1)
应用 (7)
StringReplace[" Have a nice day. ", (StartOfString ~~ Whitespace) | (Whitespace ~~ EndOfString) -> ""]//FullFormStringReplace["this (*comment1*) is a test (*comment2*)", "(*" ~~ Shortest[___] ~~ "*)" -> ""]StringCases["this (*comment1*) is a test (*comment2*)", "(*" ~~ Shortest[___] ~~ "*)"]StringReplace[
"<title>The Title</title>
<h1>The <a href='link'>head</a></h1>
<p>Some text follows here...</p>", "<" ~~ Except[">"].. ~~ ">" -> ""]StringReplace["this is a test", WordBoundary ~~ x_ :> ToUpperCase[x]]StringReplace["abc 31/12/2003 abcd 5/3/2004 abc", a : DatePattern[{"Day", "Month", "Year"}] :> DateString[DateList[{a, {"Day", "Month", "Year"}}], {"Year", "/", "Month", "/", "Day"}]]StringReplace["I have 13 licenses: 6 front end licenses and 7 kernel licenses",
WordBoundary ~~ x : DigitCharacter.. ~~ WordBoundary /; PrimeQ[ToExpression[x]] :>
"*" <> x <> "*"]ssevolveList[rule_, init_String, t_Integer] := NestList[StringReplace[#, rule]&, init, t]ssevolveList[ {"B" -> "BA", "A" -> "AB"}, "B", 5]属性和关系 (1)
StringPosition 给出每个模式出现时的起点和终点位置:
StringPosition["abbaabbaa", "ab"]输出可以和 StringReplacePart 连用:
StringReplacePart["abbaabbaa", "X", %]StringReplace["abbaabbaa", "ab" -> "X"]巧妙范例 (1)
强调文本中给定长度的单词,它摘录于 A New Kind of Science 的第一册:
text = "Just over twenty years ago I made what at first seemed like a small discovery: a computer experiment of mine showed something I did not expect. But the more I investigated, the more I realized that what I had seen was the beginning of a crack in the very foundations of existing science, and a first clue towards a whole new kind of science. This book is the culmination of nearly twenty years of work that I have done to develop that new kind of science. I had never expected it would take anything like as long, but I have discovered vastly more than I ever thought possible, and in fact what I have done now touches almost every existing area of science, and quite a bit besides.";Manipulate[TextCell[Row[List@@StringReplace[text, {RegularExpression["\\b\\w{" <> ToString[u] <> "}\\b"] :> Style["$0", 18, Bold]}]], "TR"], {u, 1, 10, 1}, SaveDefinitions -> True]历史
1991年引入 (2.0) | 在以下年份被更新:1999 (4.0) ▪ 2000 (4.1) ▪ 2002 (4.2) ▪ 2003 (5.0) ▪ 2004 (5.1) ▪ 2016 (10.4) ▪ 2020 (12.2)
文本
Wolfram Research (1991),StringReplace,Wolfram 语言函数,https://reference.wolfram.com/language/ref/StringReplace.html (更新于 2020 年).
CMS
Wolfram 语言. 1991. "StringReplace." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2020. https://reference.wolfram.com/language/ref/StringReplace.html.
APA
Wolfram 语言. (1991). StringReplace. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/StringReplace.html 年
BibTeX
@misc{reference.wolfram_2026_stringreplace, author="Wolfram Research", title="{StringReplace}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringReplace.html}", note=[Accessed: 05-December-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringreplace, organization={Wolfram Research}, title={StringReplace}, year={2020}, url={https://reference.wolfram.com/language/ref/StringReplace.html}, note=[Accessed: 05-December-2026]}