MySQL判斷中文字符的方法(轉)


准備:

2.1、環境

2.2、創建測試表和插入測試數據 

方法:

3.1、方法一:正則表達式 

3.2、方法二:length()和char_length() 

總結

方法一中,[u0391-uFFE5]匹配中文以外的字符。

方法二中,當字符集為UTF-8,並且字符為中文時,length()char_length()兩個方法返回的結果不相同。

參考官方文檔:

LENGTH()
Return the length of a string in bytes
Returns the length of the string str, measured in bytes. A multibyte character counts as multiple bytes. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

CHAR_LENGTH()
Return number of characters in argument
Returns the length of the string str, measured in characters. A multibyte character counts as a single character. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

Ref

12.5 String Functions

 

參考:

http://blog.jobbole.com/94567/(以上內容轉自此篇文章)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM