包含无法找到 VbTab & Chr

问题描述

Contains 无法找到 VbTab & Chr(...)

    Public Function GetBullet(Txt As String) As String
        GetBullet = ""
        If Txt.Contains(vbTab & Chr(149)) Then GetBullet = Chr(149)
        If Txt.Contains(vbTab & Chr(176)) Then GetBullet = Chr(176)
        If Txt.Contains(vbTab & Chr(183)) Then GetBullet = Chr(183)
        If Txt.Contains(vbTab & Chr(187)) Then GetBullet = Chr(187)
    End Function

例如:

Txt: vbTab & "・我的文字"

Asc(Mid(Txt,2,1)) 返回 183

GetBullet 在函数结束时返回 ""

这是一个错误还是我写错了什么?

解决方法

.net 中的字符串是 unicode。您发布的字符串包含字符 12539,而不是 183

If Txt.Contains(vbTab & ChrW(12539)) Then GetBullet = ChrW(12539)
                           ^                             ^
                       important                     important

换句话说:

AscW(Mid(Txt,2,1)) returns 12539
   ^
important

也许切换到现代 .net 版本而不是旧的传统 VB 辅助函数会更好:

If Txt.Contains(vbTab & Convert.ToChar(12539)) Then GetBullet = Convert.ToChar(12539)

'index strings by 0 based index to get the char at that index
'Txt(1) is the second character in Txt
Convert.ToInt32(Txt(1)) returns 12539