62,271
社区成员
发帖
与我相关
我的任务
分享
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