散分,写了一个字符串截取一定长度的代码,中文两个字符,英文一个字符,如果截断了自动用一个.或两个.来对齐,用于标题的显示

hztltgg 2009-03-26 11:03:08

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
...全文
453 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
lawbc 2009-04-07
  • 打赏
  • 举报
回复
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="value">原字符串</param>
/// <param name="length">字节长度</param>
/// <param name="encoding">字符编码</param>
/// <returns></returns>
public static string SubString(string value, int length, System.Text.Encoding encoding)
{
int count = 0;
string result = string.Empty;
foreach (char ch in value)
{
count += encoding.GetByteCount(ch.ToString());
if (count > length) break;
result += ch.ToString();
}
return result;
}
xue1234567890 2009-03-27
  • 打赏
  • 举报
回复
ding
路人乙e 2009-03-27
  • 打赏
  • 举报
回复
支持一下
tzs2304 2009-03-27
  • 打赏
  • 举报
回复
jf
cmy3918 2009-03-27
  • 打赏
  • 举报
回复
不过为什么要加".."而不是"."呢?
这样如果是"你123啊你好啊"这样的串截取长度为6
结果是"你123.."
"你好啊你好啊"这样的串截取长度为6
结果是"你好啊"
这样两个结果长度还是不一样啊..

cmy3918 2009-03-27
  • 打赏
  • 举报
回复
不错,,收藏了..
bw555 2009-03-27
  • 打赏
  • 举报
回复
up
zzxap 2009-03-27
  • 打赏
  • 举报
回复
学习
oo渣渣oo 2009-03-27
  • 打赏
  • 举报
回复
收藏了!
我姓区不姓区 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 hztltgg 的回复:]
要截取的长度比本身大,也不能返回空呀,是返回字符串不是,不过也不能直接比较长度,还是要看字符串到底有多少汉字多少字符的,比如,五个汉字,取8长度,虽然会小于,但还是要处理成三个汉字加两个点
[/Quote]
你说的很对,而且我好像理解错了,strLength 是要截取的字符数还是字节数?
  • 打赏
  • 举报
回复
jf
ty850818 2009-03-27
  • 打赏
  • 举报
回复
jf
jlj84237485 2009-03-27
  • 打赏
  • 举报
回复
接分
josephSC 2009-03-27
  • 打赏
  • 举报
回复
up, jf~
我姓区不姓区 2009-03-27
  • 打赏
  • 举报
回复
总算看懂你的意思了,修改一下:

Public Function HalfSubstring(ByVal str As String, ByVal strLength As Integer) As String
If System.Text.Encoding.Unicode.GetByteCount(str) < strLength Then
Return str
End If
Dim bytesStr() As Byte = System.Text.Encoding.Unicode.GetBytes(str)
Dim list As List(Of Byte) = New List(Of Byte)()
Dim count As Integer = 0
For i As Integer = 0 To bytesStr.Length - 1 Step 2
If count = strLength Then
Exit For
End If
If bytesStr(i + 1) = 0 Then
If count + 1 = strLength Then
list.Add(46)
list.Add(0)
count += 1
Else
list.Add(bytesStr(i))
list.Add(bytesStr(i + 1))
count += 1
End If
Else
If count + 2 > strLength Then
list.Add(46)
list.Add(0)
count += 1
ElseIf count + 2 = strLength Then
list.Add(46)
list.Add(0)
list.Add(46)
list.Add(0)
count += 2
Else
list.Add(bytesStr(i))
list.Add(bytesStr(i + 1))
count += 2
End If
End If
Next
Return System.Text.Encoding.Unicode.GetString(list.ToArray())
End Function

hztltgg 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 cmy3918 的回复:]
不过为什么要加".."而不是"."呢?
这样如果是"你123啊你好啊"这样的串截取长度为6
结果是"你123.."
"你好啊你好啊"这样的串截取长度为6
结果是"你好啊"
这样两个结果长度还是不一样啊..
[/Quote]

我的代码是会根据情况加一个或者两个点的哦

你的例子结果是
你123.
你好..

一样长的
hztltgg 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 ojlovecd 的回复:]
引用 14 楼 hztltgg 的回复:
要截取的长度比本身大,也不能返回空呀,是返回字符串不是,不过也不能直接比较长度,还是要看字符串到底有多少汉字多少字符的,比如,五个汉字,取8长度,虽然会小于,但还是要处理成三个汉字加两个点

你说的很对,而且我好像理解错了,strLength 是要截取的字符数还是字节数?
[/Quote]

我试了你的代码,好像输出有点问题的,strlength是按半角为单位的长度,英文算一个,中文算两个。
limpid_123 2009-03-26
  • 打赏
  • 举报
回复
顶啊~~~~~~~~~~
qshurufa 2009-03-26
  • 打赏
  • 举报
回复
接分。
a854468521 2009-03-26
  • 打赏
  • 举报
回复
顶。
我穷啊!
加载更多回复(11)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧