写了一个字符串截取一定长度的代码,中文两个字符,英文一个字符,如果截断了自动用一个.或两个.来对齐,用于标题的显示
2009-03-26 08:59
946 查看
Imports System.Runtime.CompilerServices
Public Module StringExtensions
<Extension()> _
Public Function HalfSubstring(ByVal str As String, ByVal strLength As Integer) As String
strLength = strLength - 1
Dim newStr As String = String.Empty
Dim p As Integer
For i As Integer = 0 To str.Length - 1
Dim subStr As String = str.Substring(i, 1)
If (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
p += 2
newStr &= subStr
Else
p += 1
newStr &= subStr
End If
If p > strLength Then
If p > strLength + 1 And (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & "."
Exit For
Else
If (Regex.IsMatch(newStr.Substring(newStr.Length - 1, 1), "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & ".."
Else
newStr = newStr.Substring(0, newStr.Length - 1) & "."
End If
Exit For
End If
End If
Next
Return newStr
End Function
End Module
Public Module StringExtensions
<Extension()> _
Public Function HalfSubstring(ByVal str As String, ByVal strLength As Integer) As String
strLength = strLength - 1
Dim newStr As String = String.Empty
Dim p As Integer
For i As Integer = 0 To str.Length - 1
Dim subStr As String = str.Substring(i, 1)
If (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
p += 2
newStr &= subStr
Else
p += 1
newStr &= subStr
End If
If p > strLength Then
If p > strLength + 1 And (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & "."
Exit For
Else
If (Regex.IsMatch(newStr.Substring(newStr.Length - 1, 1), "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & ".."
Else
newStr = newStr.Substring(0, newStr.Length - 1) & "."
End If
Exit For
End If
End If
Next
Return newStr
End Function
End Module
相关文章推荐
- 截取文本,区分中英文字符,中文算两个长度,英文算一个长度
- JS获取字符串长度,一个中文算两个字符。
- jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- javascript 返回字符长度,中文为两个字节,英文为一个字节
- 计算文本长度,区分中英文字符,中文算两个长度,英文算一个长度
- 截取字符串 包含中文处理,一个中文字符等于2个长度
- php取含有中文的字符串的真实长度(一个中文算两个字符)
- 将字符串按照指定的长度进行分割(一个中文视为两个字符)
- 字符串长度及截取(中文2字符,英文1字符)
- 用JavaScript截取一定长度字符串(中文长度为2,英文长度为1)
- JS获取字符串长度(中文占两个,数字与英文占一个)
- 两个截取字符串的实用方法(超过一定长度自动换行)
- javascript计算中文字符长度(一个中文字符相当于两个英文字符)
- python 计算字符串长度,一个中文算两个字符
- 截取字符串,中文一个字算一个字,英文两个字符算一个字
- 字符串截取,一个中文算两个字符
- JS 判断输入字符串的长度(中文占用两个字节,英文占用一个字节)
- js字符串长度计算(一个汉字==两个字符)和字符串截取
- 计算一段字符串的长度是否超出规定字数,两个英文字符占一个长度。