我利用left函数截取前5个字符,如left("中国人民解放军")=中国人民解,但是如果字符串带有数字或英文字母,如left("中国12345678")=中国123。我的问题是:
能不能让两个字母或两个数字算一个,如left("中国12345678")=中国123456。
望各位能够帮助!!!
我会及时给分的。
能不能让两个字母或两个数字算一个,如left("中国12345678")=中国123456。
望各位能够帮助!!!
我会及时给分的。
int byteCount=System.Text.Encoding.Default.GetByteCount(s);byteCount得到字符串变量里面的字符串所占字节数。
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
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
yourString = Cut_String(yourString,5,0)
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
如果我想解决当前一个字符串的前20个字符串来显示(因为显示太多就影响页面美观),可是界若第20和21个字符(一个汉字算作两个字符)是一个汉字,如果直接进行截取一定会出错,请问如何解决? 期待大虾们的高见 ^_^
上面这行代码是为了回答你的问题而加上去的,原来的程序里没有。因为你要求的既不是按字符个数截取,也不是按字节数截取,而是2个字节2个字节那样的截取,所以加了这么一行代码。