我利用left函数截取前5个字符,如left("中国人民解放军")=中国人民解,但是如果字符串带有数字或英文字母,如left("中国12345678")=中国123。我的问题是:
能不能让两个字母或两个数字算一个,如left("中国12345678")=中国123456。
望各位能够帮助!!!
我会及时给分的。

解决方案 »

  1.   

    给你个例子:string s="abc村械地23423";
    int byteCount=System.Text.Encoding.Default.GetByteCount(s);byteCount得到字符串变量里面的字符串所占字节数。
      

  2.   

    string s = "歇息xx下";
    byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(s);
    this.Page.Response.Write(b.Length);//8 s = System.Text.Encoding.GetEncoding("gb2312").GetString(b,0,6);
    this.Page.Response.Write(s);//歇息xx
      

  3.   


    Private Function Cut_String(ByVal sSource As String, ByVal iByte As Integer, ByRef iMid As Integer) As String
    Try
    Dim byteSource() As Byte
    Dim CutsSource As String
    Dim sReturn As String = String.Empty
    Dim sLength As Integer
    sLength = sSource.Length
    iByte = iByte * 2
    If iByte > sLength - iMid Then
    CutsSource = sSource.Substring(iMid, sLength - iMid)
    Else
    CutsSource = sSource.Substring(iMid, iByte)
    End If
    byteSource = System.Text.Encoding.Default.GetBytes(CutsSource)
    ReDim Preserve byteSource(iByte - 1)
    sReturn = System.Text.Encoding.Default.GetString(byteSource)
    iMid = iMid + sReturn.Length
    Return sReturn
    Catch je As JediApplException
    Throw je
    Catch ex As Exception
    Throw New JediApplException(SYS_ERR, Me, ex)
    End Try
    End Functionprivate yourSub()         dim yourString = "中国12345678"
             yourString = Cut_String(yourString,6,0)
             messagebox.show(yourString)  '这时yourString应该是“中国123456”end sub
      

  4.   

    yourString = Cut_String(yourString,6,0)这句错了。如果要取“中国123456”的话,应该是:
    yourString = Cut_String(yourString,5,0)
      

  5.   

    完整修正版,haha^^Private Function Cut_String(ByVal sSource As String, ByVal iByte As Integer, ByRef iMid As Integer) As String
    Try
    Dim byteSource() As Byte
    Dim CutsSource As String
    Dim sReturn As String = String.Empty
    Dim sLength As Integer
    sLength = sSource.Length
    iByte = iByte * 2
    If iByte > sLength - iMid Then
    CutsSource = sSource.Substring(iMid, sLength - iMid)
    Else
    CutsSource = sSource.Substring(iMid, iByte)
    End If
    byteSource = System.Text.Encoding.Default.GetBytes(CutsSource)
    ReDim Preserve byteSource(iByte - 1)
    sReturn = System.Text.Encoding.Default.GetString(byteSource)
    iMid = iMid + sReturn.Length
    Return sReturn Catch ex As Exception
             Throw ex 
    End Try
    End Functionprivate yourSub()         dim yourString = "中国12345678"
             yourString = Cut_String(yourString,5,0)    '〈--- 这里添5
             messagebox.show(yourString)  '这时yourString应该是“中国123456”end sub
      

  6.   

    to  brightheroes(闭关|那一剑的风情) :楼主的问题是解决了,但是我还有一个问题:
        如果我想解决当前一个字符串的前20个字符串来显示(因为显示太多就影响页面美观),可是界若第20和21个字符(一个汉字算作两个字符)是一个汉字,如果直接进行截取一定会出错,请问如何解决?     期待大虾们的高见  ^_^
      

  7.   

    答楼主:不谢,能帮忙就帮忙而已。这个Cut_String函数是最近我做的一个VB。NET的工程用到的,它用在一个数据通信的功能里,说白了就是传递字符串流。一个字符串流里面包含多个记录,每条记录的大小确定,512 Bytes,但是这记录里面是汉字和数字字母混合的文本,接收到这个字符串流以后,需要按字节数切割这条记录里的每一个列的数值。当然,如果只是按照字符个数截取的话,很容易实现,subString就行,但是如果按字节数截取就相对麻烦一些,所以我写了上面的这个Cut_String函数作为公共函数来调用。iByte = iByte * 2
    上面这行代码是为了回答你的问题而加上去的,原来的程序里没有。因为你要求的既不是按字符个数截取,也不是按字节数截取,而是2个字节2个字节那样的截取,所以加了这么一行代码。