# 4.5 字符串比较

Function: char-equal character1 character2

当两个参数表示同一字符时，该函数返回 t，否则返回 nil。该函数会忽略大小写，除非变量 case-fold-search 的值为 non-nil

```
(char-equal ?x ?x)
     ⇒ t
(let ((case-fold-search nil))
  (char-equal ?x ?X))
     ⇒ nil
```

Function: string= string1 string2

若两个字符串中的字符完全匹配，则返回 t。符号同样可以作为参数传入，此时会使用该符号的名称。该函数对大小写敏感，除非变量 case-fold-search 为 non-nil。

该函数在比较字符串时，和函数 equal 时等价的（查阅相等谓词）。更细一点说，在比较字符串时，会忽略字符串的文本属性。如果你想只比较文本属性，请使用函数 equal-including-propertues。和 equal 不同的地方在于，若参数既不是字符串，也不是符号，则会抛出一个错误。

```
(string= "abc" "abc")
     ⇒ t
(string= "abc" "ABC")
     ⇒ nil
(string= "ab" "ABC")
     ⇒ nil
```

由于技术原因，单字节字符串和多字节字符串当且仅当它们包含的字符序列完全相同，且这些字符的范围在0到127（ASCII）或160到255（八位图）之间。不过需要注意的是，当单字节字符串转换为多字节字符串时，所有在160到255之间的字符都会被转换为对应的，更高的字符码，而ASCII码不会改变。因此，单字节字符串只有在其组成字符全部为ASCII码时，才会和对应转换的多字节字符串 equal。而在160到255之间的字符却不一定一致。因此，包含这一段字符的单字节字符串和多字节字符串的比较可能会出现很多奇怪的现象，不过这种现象很少出现。查阅文本表示章节。

Function: string-equal string1 string2

该函数是函数 string= 的别名。

Function: string-collate-equalp string1 string2 \&optional locale ignore-case

若 string1 和 string2 在 collate 规则下 equal，则该函数返回 t。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/zi-fu-chuan-he-zi-fu/zi-fu-chuan-bi-jiao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
