Classic ASP中如何将长字符串转换为6行块格式段落?
嘿,在Classic ASP里实现这个需求其实挺简单的,根据你的场景我给你几个实用的方案:
方案1:手动语义拆分(适合固定文本)
如果你的message内容是固定不变的,直接按语义把字符串拆成6行是最精准的方式,完全不用担心断句问题:
<% Dim message, formattedMessage message = "This is a story of a little boy visiting the sweetshop for the first time, this was a very exciting time for him as he's never been in a sweet shop before and it was full of exciting thing to see and do, but he wasn't allowed in the shop without his mummy." ' 按语义手动拆成6行,用vbCrLf作为换行符(文本输出) formattedMessage = "This is a story of a little boy visiting the sweetshop for the first time," & vbCrLf & _ "this was a very exciting time for him" & vbCrLf & _ "as he's never been in a sweet shop before" & vbCrLf & _ "and it was full of exciting thing to see and do," & vbCrLf & _ "but he wasn't allowed in the shop" & vbCrLf & _ "without his mummy." ' 如果是网页上显示,把换行符替换成HTML的<br>标签 ' formattedMessage = Replace(formattedMessage, vbCrLf, "<br>") Response.Write(formattedMessage) %>
方案2:自动按字符数均分(适合动态内容)
如果message是动态生成的,没法手动拆分,可以按总长度均分6份,自动生成每行内容:
<% Dim message, formattedMessage, lineCount, charsPerLine, i, startPos message = "This is a story of a little boy visiting the sweetshop for the first time, this was a very exciting time for him as he's never been in a sweet shop before and it was full of exciting thing to see and do, but he wasn't allowed in the shop without his mummy." lineCount = 6 charsPerLine = Round(Len(message) / lineCount) ' 计算每行平均字符数 formattedMessage = "" startPos = 1 For i = 1 To lineCount ' 最后一行直接取剩余所有内容 If i = lineCount Then formattedMessage = formattedMessage & Mid(message, startPos) Else formattedMessage = formattedMessage & Mid(message, startPos, charsPerLine) & vbCrLf startPos = startPos + charsPerLine End If Next ' 网页显示替换换行符为<br> ' formattedMessage = Replace(formattedMessage, vbCrLf, "<br>") Response.Write(formattedMessage) %>
方案3:优化版自动拆分(避免单词截断)
方案2可能会把单词从中间截断,比如把“sweetshop”拆成两行,这个优化版会自动找到每行末尾最近的空格,保证单词完整:
<% Dim message, formattedMessage, lineCount, targetLen, i, startPos, endPos message = "This is a story of a little boy visiting the sweetshop for the first time, this was a very exciting time for him as he's never been in a sweet shop before and it was full of exciting thing to see and do, but he wasn't allowed in the shop without his mummy." lineCount = 6 targetLen = Round(Len(message) / lineCount) formattedMessage = "" startPos = 1 For i = 1 To lineCount ' 最后一行直接取剩余内容 If i = lineCount Then formattedMessage = formattedMessage & Mid(message, startPos) Exit For End If ' 先定位到目标长度位置,再往前找最近的空格 endPos = startPos + targetLen - 1 If endPos < Len(message) Then Do While endPos > startPos And Mid(message, endPos, 1) <> " " endPos = endPos - 1 Loop End If formattedMessage = formattedMessage & Mid(message, startPos, endPos - startPos + 1) & vbCrLf startPos = endPos + 2 ' 跳过空格,定位到下一行起始位置 Next ' 网页显示替换换行符为<br> ' formattedMessage = Replace(formattedMessage, vbCrLf, "<br>") Response.Write(formattedMessage) %>
总结
- 固定文本选方案1:语义最通顺,实现最简单;
- 动态文本选方案3:既自动拆分,又保证单词完整,体验更好。
内容的提问来源于stack exchange,提问作者Rick Kap
相关产品推荐
相关产品推荐

