在新聞文章項目里經常會碰到在前臺顯示標題列表,因為前臺界面都是固定好了的,所以在顯示時必須限制標題顯示字符數(shù)量,超過這個數(shù)字的字符將被截去,以“...”之類的代替。
截取字符串我們一般最常用也最簡單的就是用string的
Substring方法,這種方法在取值時漢字字母是不區(qū)分的,而因為漢字跟字母在顯示時寬度有很大區(qū)別,所以截取后相同長度字符時有字母數(shù)字跟不帶字母數(shù)字的字符串顯示上回有很大區(qū)別,從而影響整個排版,比如下面的字符串:
繁時不可慌,閑時不可荒
fsbkh,xsbkh
同為相同字符數(shù),顯示時就極不對稱了。
另外一種方法就是,把字符串轉化為bytes,在計算字符串的byte數(shù)時一個漢字按兩個byte處理的,所以在截取字符時就避免了漢字字母顯示時的不對稱,以下自己寫的一個標題字符串截取函數(shù): 截取字符串長度
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()Function CutString(ByVal str As String, ByVal len As Integer, ByVal strMore As String) As 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, 0, len - strMoreLength) & strMore
14 Else
15 Return str
16 End If
17 End Function 上面這個方法也很簡單,但是還有個問題,就是如果我要處理的字符串是“520字符串截取”,按上面的方法,它的GetBytes.Length應該等于13,如果我要截取的長度len=10的話,那么它截取后的字符串就成了“520字符串?”因為sarr(9)即第十個byte的值其實是表示"截"字的兩個byte中的一個,這樣就會出現(xiàn)文字顯示錯誤。
看到另外一種方法,先用asc()得到字符的ascii碼,如果asc()<0就是漢字,這時長度就算2,否子算一個字符長度,這種方法就避免了上面的截取字符串造成字符丟失的情況:
Code
1Public Shared Function LeftByte()Function LeftByte(ByVal Str As String, ByVal Lens As Double) As 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)) < 0) Then
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 綜合以上一些方法,自己寫了個針對處理字符串的函數(shù):超過規(guī)定長度時即截取,省略的字符做參數(shù)添加,比如想在省略的字符處顯示"……"或"..."或"--"都可以,免去了每個顯示都要去重寫一次方法的煩瑣:
Code
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> 區(qū)分漢字數(shù)字字母的長短問題,超過固定長度顯示固定字符串替代</remarks>
11 Public Shared Function CutStringByByte()Function CutStringByByte(ByVal Str As String, ByVal Lens As Integer, ByVal StrMore As String) As 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)) < 0) Then
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)) < 0) Then
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