33.20 字符编码替换

以下函数根据字符编码替换指定区域内的字符。

Function: subst-char-in-region start end old-char new-char &optional noundo

该函数将当前缓冲区中由 startend 定义的区域内,所有 old-char 字符替换为 new-char 字符。两个字符的多字节形式长度必须相同。

如果 noundonil,则 subst-char-in-region 不会为撤销操作记录此次修改,也不会将缓冲区标记为已修改。这一特性曾用于控制旧版的选择性显示功能(see 选择性显示)。

subst-char-in-region 不会移动点的位置,返回值为 nil

---------- Buffer: foo ----------
This is the contents of the buffer before.
---------- Buffer: foo ----------

(subst-char-in-region 1 20 ?i ?X)
     ⇒ nil

---------- Buffer: foo ----------
ThXs Xs the contents of the buffer before.
---------- Buffer: foo ----------
Function: subst-char-in-string fromchar tochar string &optional inplace

该函数将 string 中所有 fromchar 字符替换为 tochar 字符。默认情况下,替换操作在 string 的副本中进行;但若可选参数 inplacenil,函数会直接修改 string 本身。无论哪种情况,函数都会返回替换后的字符串。

Command: translate-region start end table

该函数对缓冲区中 startend 位置之间的字符应用转换表。

转换表 table 可以是字符串或字符表;(aref table ochar) 会返回与原字符 ochar 对应的转换后字符。如果 table 是字符串,编码值大于该字符串长度的字符不会被转换。

translate-region 的返回值是实际被转换修改的字符数量,转换表中映射为自身的字符不计入在内。