VB6判斷字串中是否包含漢字

2021-08-25 10:00:25 字數 1411 閱讀 4101

對於google中和csdn中沒有正確嚴謹的vb6判斷字串中是否包含漢字,要麼簡單的判斷是否asc()返回值小於0(這樣只能判斷是否為雙位元組的字串,比如韓文、日文、中文、阿拉伯文,無法確定為中文),難怪人家說我們vb6的都是初級新手用的。判斷漢字的做法,都如此不嚴謹。

給出乙個比較嚴謹的判斷是否包含中文方法好了。希望日後有人搜尋到,能夠有幫助。

建立窗體,放乙個按鈕,預設名字,貼上如下**在**頁面。

private sub command1_click() dim s1, s2, s3, s4 as string s1 = "中文" s2 = "ち" s3 = "english" s4 = "123" dim result as string result = """" + s1 + """ " + iif(includechinese(s1), "包含漢字", "不包含漢字") result = result + vbcrlf + """" + s2 + """ " + iif(includechinese(s2), "包含漢字", "不包含漢字") result = result + vbcrlf + """" + s3 + """ " + iif(includechinese(s3), "包含漢字", "不包含漢字") result = result + vbcrlf + """" + s4 + """ " + iif(includechinese(s4), "包含漢字", "不包含漢字") msgbox result end sub '判斷是否字串中包含漢字 public function includechinese(byval s as string) as boolean '定義位元組陣列指向字串 dim data() as byte data = s '臨時變數,integer長度為2,正好用來判斷是否是漢字,因為漢字是雙位元組的 dim t as integer '定義迴圈變數 dim i as integer '漢字邊界值 '因為直接寫&h9fa5會認為成integer的負數,所以要寫成字串,進行轉換 dim lb, ub as long lb = clng("&h4e00") '漢字最小值 ub = clng("&h9fa5") '迴圈判斷,漢字的範圍是4e00 ~ 9fa5 '因為我不太懂vb6的技巧寫法,就用乙個比較繁瑣的寫法做型別轉換 for i = 0 to ubound(data) step 2 t = clng("&h" + hex(data(i) + data(i + 1) * 255)) '連續的2個位元組拼接為乙個integer if t >= lb and t <= ub then includechinese = true '找到任何乙個漢字則返回true exit function end if next includechinese = false '不包含漢字 end function

輸出結果:

"中文" 包含漢字

"ち" 不包含漢字

"english" 不包含漢字

"123" 不包含漢字

判斷字串 python判斷字串是否包含字母

第一種方法 使用正規表示式判斷字串是否包含字母 coding utf 8 import re def check str my re re.compile r a za z re.s res re.findall my re,str if len res print u 含有英文本元 else pr...

php判斷字串中是否包含指定字串

兩種思路兩種函式 第一 判斷字串中是否包含指定字串 第二 判斷字串中是否出現指定字串 第一種是strpos 查詢字串在另外乙個字串出現的位置,如果沒有,返回false a a b abbb if strpos b,a false else上邊 返回的是存在,需要注意的是strpos是敏感的,區分大小...

判斷字串是否包含漢子

如下 using system using system.collections.generic using system.linq using system.text using system.text.regularexpressions 判斷表示是否全為英文 判斷字串中是否包含中文 需要判斷的...