精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● VB和Basic>>〓〓..各种开发技巧..〓〓>>系统和硬件操作>>Re:看似简单,但又难做到的问题.

主题:Re:看似简单,但又难做到的问题.
发信人: kingywq()
整理人: winsy(2003-03-06 09:05:41), 站内信件
【 在 xiaolong 的大作中提到:】
:
:要求是这样的。有字符串"小龙1小龙12小"
:我需要每六个字节分段截取字符串,作用是打印时的自动分行。
:例如截取后的内容如下:
:123456 <-意思为六个字节位置,一个汉字占两个字节
:
:......
 
    其实,并不一定需要很深入的了解Windows如何对汉字进行编码的。我们只要知道单字节字符的ASC()函数值大于0,而中文等双字节字符的ASC()函数值小于0,再进行相应处理就可以了。我在一个EXCEL的VBA程序中处理过这个问题。以下是一些子程序,希望能对你有所帮助。

    你根据以下子程序的编程思想,应该不难写出你需要的代码。

'本函数返回一字节计算的参数字符串sInput的大小    
Public Function ByteLen(ByVal sInput As String) As Integer
    Dim iLoop, iLen As Integer
    Dim sTemp As String
    
    iLen = 0
    If sInput = "" Then
        ByteLen = 0
    Else
        For iLoop = 1 To Len(sInput)
            sTemp = Mid(sInput, iLoop, 1)
            If Asc(sTemp) >= 0 Then    'ASC函数值不小于0,表明为单字节字符
                iLen = iLen + 1
            Else    ''ASC函数值小于0,表明为双字节字符
                iLen = iLen + 2
            End If
        Next iLoop
        ByteLen = iLen
    End If
End Function

'该函数返回参数字符串sString的左边iLen个字节的字符
Public Function ByteLeft(ByVal sString As String, ByVal iLen As Integer) As String
    Dim sTemp, sReturn As String
    Dim iLoop, iCount As Integer
    
    sTemp = sString
    sReturn = ""
    iCount = 0
    For iLoop = 1 To iLen
        sReturn = sReturn + Left(sTemp, 1)
        iCount = iCount + ByteLen(Left(sTemp, 1))
        sTemp = Mid(sTemp, 2)
        If iCount >= iLen Then
            Exit For
        End If
    Next iLoop
    ByteLeft = sReturn
End Function

'该函数返回参数字符串sString的从iStart开始的iLen个字节的字符
Public Function ByteMid(ByVal sString As String, ByVal iStart As Integer, ByVal iLen As Integer) As String
    Dim sTemp As String
    Dim iLoop, iCount As Integer
    
    sTemp = sString
    iCount = 0
    For iLoop = 1 To iStart - 1
        iCount = iCount + ByteLen(Left(sTemp, 1))
        sTemp = Mid(sTemp, 2)
        If iCount >= iStart - 1 Then
            Exit For
        End If
    Next iLoop
    
    ByteMid = ByteLeft(sTemp, iLen)
End Function

[关闭][返回]