Chinaunix首页 | 论坛 | 博客
  • 博客访问: 734738
  • 博文数量: 769
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 4985
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 16:37
文章分类

全部博文(769)

文章存档

2011年(1)

2008年(768)

我的朋友

分类:

2008-10-15 16:37:23

    在新闻文章项目里经常会碰到在前台显示标题列表,因为前台界面都是固定好了的,所以在显示时必须限制标题显示字符数量,超过这个数字的字符将被截去,以“……”之类的代替。

    截取字符串我们一般最常用也最简单的就是用string的Substring方法,这种方法在取值时汉字字母是不区分的,而因为汉字跟字母在显示时宽度有很大区别,所以截取后相同长度字符时有字母数字跟不带字母数字的字符串显示上回有很大区别,从而影响整个排版,比如下面的字符串:繁时不可慌,闲时不可荒fsbkh,xsbkh同为相同字符数,显示时就极不对称了。

    另外一种方法就是,把字符串转化为bytes,在计算字符串的byte数时一个汉字按两个byte处理的,所以在截取字符时就避免了汉字字母显示时的不对称,以下自己写的一个标题字符串截取函数:

 
 1 ''' 
 2    ''' 截取字符串长度
 3    ''' 

 4    ''' 待处理的字符串
 5    ''' 截取长度
 6    ''' 超过长度部分显示字符
 7    ''' String
 8    ''' 超过固定长度显示固定字符串替代

 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  ''' 
 2    ''' 截取字符串长度
 3    ''' 

 4    '''TheoMi
 5    '''2008-03-15
 6    ''' 待处理的字符串
 7    ''' 截取长度
 8    ''' 超过长度部分显示字符
 9    ''' String
10    '''  区分汉字数字字母的长短问题,超过固定长度显示固定字符串替代

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

【责编:Ken】

--------------------next---------------------
 1  ''' 
 2    ''' 截取字符串长度
 3    ''' 

 4    '''TheoMi
 5    '''2008-03-15
 6    ''' 待处理的字符串
 7    ''' 截取长度
 8    ''' 超过长度部分显示字符
 9    ''' String
10    '''  区分汉字数字字母的长短问题,超过固定长度显示固定字符串替代

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

--------------------next---------------------

阅读(352) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~