VB.net(VB6)String类型、中文、日文与Unicode互相转换

VB.net(VB6)String类型、中文、日文与Unicode互相转换

原文地址:https://blog.csdn.net/boys1999/article/details/23214065

首先添加引用

Imports System.Text.RegularExpressions

①将unicode转成中文,如:ABC \u8033\u9EA6 12345,转后为:ABC 耳麦 12345

 '将unicode转成中文,如:ZRO \u8033\u9EA6 12345,转后为:ZRO 耳麦 12345
    Public Function UnicodeToString(strCode As String) As String
        UnicodeToString= strCode
        If InStr(UnicodeToString, "\u") <= 0 Then
            Exit Function
        End If
        strCode = LCase(strCode)
        Dim mc As MatchCollection
        mc = Regex.Matches(strCode, "\\u\S{1,4}")
        For Each m In mc
            strCode = Replace(strCode, m.ToString, ChrW("&H" & Mid(CStr(m.ToString), 3, 6)))
        Next
        UnicodeToString= strCode
    End Function

②将中文转为unicode编码,如:ABC 耳麦 12345,转后为:ABC \u8033\u9EA6 12345

'将中文转为unicode编码,如:ZRO 耳麦 12345,转后为:ZRO \u8033\u9EA6 12345
Function StringToUnicode(strCode As String) As String
    Dim a() As String
    Dim str As String
    Dim i As Integer
    For i = 0 To Len(strCode) - 1
        On Error Resume Next
        str = Mid(strCode, i + 1, 1)
        If isChinese(str) = True Then '//是中文
            StringToUnicode= StringToUnicode & "\u" & StrDup(4 - Len(Hex(AscW(str))), "0") & Hex(AscW(str))
        Else '//不是中文
            StringToUnicode= StringToUnicode & str
        End If
    Next
End Function

'是否为中文
Public Function isChinese(Text As String) As Boolean

    Dim l As Long
    Dim i As Long
    l = Len(Text)
    isChinese = False

    For i = 1 To l
        If Asc(Mid(Text, i, 1)) < 0 Or Asc(Mid(Text, i, 1)) < 0 Then
            isChinese = True
            Exit Function
        End If
    Next

End Function
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值