截取字符串方法总结(区分汉字、数字、字母)

         在新闻文章项目里经常会碰到在前台显示标题列表,因为前台界面都是固定好了的,所以在显示时必须限制标题显示字符数量,超过这个数字的字符将被截去,以“...”之类的代替。          截取字符串我们一般最常用也最简单的就是用string的 Substring方法,这种方法在取值时汉字字母是不区分的,而因为汉字跟字母在显示时宽度有很大区别,所以截取后相同长度字符时有字母数字跟不带字母数字的字符串显示上回有很大区别,从而影响整个排版,比如下面的字符串:           繁时不可慌,闲时不可荒           fsbkh,xsbkh 同为相同字符数,显示时就极不对称了。                 另外一种方法就是,把字符串转化为bytes,在计算字符串的byte数时一个汉字按两个byte处理的,所以在截取字符时就避免了汉字字母显示时的不对称,以下自己写的一个标题字符串截取函数:
 1 ''' <summary>  2    ''' 截取字符串长度  3    ''' </summary>  4    ''' <param name="str">待处理的字符串</param>  5    ''' <param name="len">截取长度</param>  6    ''' <param name="strMore">超过长度部分显示字符</param>  7    ''' <returns>String</returns>  8    ''' <remarks>超过固定长度显示固定字符串替代</remarks>  9    Public Function CutString(ByVal str As StringByVal len As IntegerByVal strMore As StringAs String 10        Dim sarr As Byte() = System.Text.Encoding.Default.GetBytes(str) 11        Dim strMoreLength As Integer = System.Text.Encoding.Default.GetBytes(strMore).Length 12        If sarr.Length > len Then 13            Return System.Text.Encoding.Default.GetString(sarr, 0len - strMoreLength) & strMore 14        Else 15            Return str 16        End If 17    End Function
    上面这个方法也很简单,但是还有个问题,就是如果我要处理的字符串是“520字符串截取”,按上面的方法,它的GetBytes.Length应 该等于13,如果我要截取的长度len=10的话,那么它截取后的字符串就成了“520字符串?”因为sarr(9)即第十个byte的值其实是表示"截 "字的两个byte中的一个,这样就会出现文字显示错误。     看到另外一种方法,先用asc()得到字符的ascii码,如果asc()<0就是汉字,这时长度就算2,否子算一个字符长度,这种方法就避免了上面的截取字符串造成字符丢失的情况:
 1Public Shared Function LeftByte(ByVal Str As StringByVal Lens As DoubleAs String  2        Dim Length  3        Length = 0  4        Dim i As Integer  5        For i = 1 To Len(Str)  6            If (Asc(Mid(Str, i, 1)) < 0Then  7                Length = Length + 2  8            Else  9                Length = Length + 1 10            End If 11            If Length = Lens Then 12                LeftByte = Left(Str, i) 13                Exit Function 14            ElseIf Length > Lens Then 15                LeftByte = Left(Str, i - 1) 16                Exit Function 17            End If 18        Next 19        Return Str 20    End Function
        综合以上一些方法,自己写了个针对处理字符串的函数:超过规定长度时即截取,省略的字符做参数添加,比如想在省略的字符处显示"……"或"..."或"--"都可以,免去了每个显示都要去重写一次方法的烦琐:         
 1  ''' <summary>  2    ''' 截取字符串长度  3    ''' </summary>  4    '''<Author>TheoMi</author>  5    '''<date>2008-03-15</date>  6    ''' <param name="Str">待处理的字符串</param>  7    ''' <param name="Lens">截取长度</param>  8    ''' <param name="StrMore">超过长度部分显示字符</param>  9    ''' <returns>String</returns> 10    ''' <remarks> 区分汉字数字字母的长短问题,超过固定长度显示固定字符串替代</remarks> 11    Public Shared Function CutStringByByte(ByVal Str As StringByVal Lens As IntegerByVal StrMore As StringAs String 12        Dim Length As Integer = 0 13        Dim strBytesLength As Integer = System.Text.Encoding.Default.GetBytes(Str).Length 14        Dim MoreLength As Integer = System.Text.Encoding.Default.GetBytes(StrMore).Length 15        Dim i As Integer 16        If strBytesLength > Lens Then 17            For i = 1 To Len(Str) 18                If (Asc(Mid(Str, i, 1)) < 0Then 19                    Length = Length + 2 20                Else 21                    Length = Length + 1 22                End If 23                If Length = Lens - MoreLength Then 24                    CutStringByByte = Left(Str, i) & StrMore 25                    Exit Function 26                ElseIf Length > Lens - MoreLength Then 27                    CutStringByByte = Left(Str, i - 1& StrMore 28                    Exit Function 29                End If 30            Next 31        Else 32            For i = 1 To Len(Str) 33                If (Asc(Mid(Str, i, 1)) < 0Then 34                    Length = Length + 2 35                Else 36                    Length = Length + 1 37                End If 38                If Length = Lens Then 39                    CutStringByByte = Left(Str, i) 40                    Exit Function 41                ElseIf Length > Lens Then 42                    CutStringByByte = Left(Str, i - 1) 43                    Exit Function 44                End If 45            Next 46        End If 47        Return Str 48    End Function
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值