我刚刚编写了以下代码,但是我想知道是否有一种简单的方法来显示块格式的消息。
基本上我有一个名为message的字符串,其中包含一个句子:
"这是一个小男孩第一次来到甜品店的故事 时间,这对他来说是一个非常激动人心的时刻,因为他从未参加过 以前的甜品店,看到和做的东西都很刺激,但是 没有他的木乃伊,他不允许进入商店。"
现在我需要将它放入6行的块段中,所有40个字符(ish)每行必须以完整的单词结尾。
我使用了MID,REPLACE,DO& LOOP实现了这个功能,但我想知道是否有一种简单的方法。
主检查器是
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_1 = Mid(message,1,line_counter)
Else
line_1 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
我这样做了6次并将每行的结果存储在一个名为line _ *的变量中。
所以我的问题是,有没有更简单的方法呢?
此致
瑞克
这是代码。
<%
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."
Response.write message
Response.write "</P>"
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_1 = Mid(message,1,line_counter)
Else
line_1 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_2 = Mid(message,1,line_counter)
Else
line_2 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_3 = Mid(message,1,line_counter)
Else
line_3 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_4 = Mid(message,1,line_counter)
Else
line_4 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_5 = Mid(message,1,line_counter)
Else
line_5 = Mid(message,1,40)
End If
message = Replace(message,"","",line_counter)
line_counter = 40
If Mid(message,line_counter,1) <> " " Then
Do until Mid(message,line_counter,1) = " "
line_counter = line_counter + 1
Loop
line_6 = Mid(message,1,line_counter)
Else
line_6 = Mid(message,1,40)
End If
Response.write "</P>"
Response.write line_1
Response.write "</P>"
Response.write line_2
Response.write "</P>"
Response.write line_3
Response.write "</P>"
Response.write line_4
Response.write "</P>"
Response.write line_5
Response.write "</P>"
Response.write line_6
%>
答案 0 :(得分:0)
你应该能够使用CSS非常接近你想要的东西。您可以在width
中使用ems
的组合来限制每行的字符数,并overflow-wrap: normal
强制每行仅在您有完整的单词时进行换行。
像这样;
<%
Dim message: 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."
%>
<!doctype html>
<html>
<head>
<title>Paragraph wrapping test</title>
<style>
p.block {
width: 40em;
overflow-wrap: normal;
}
</style>
</head>
<body>
<p class="block"><%= message %></p>
</body>
</html>
静态演示:
p.block {
width: 40em;
overflow-wrap: normal;
}
<p class="block">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.</p>
另一种代替em
的方法是ch
,它在MDN上定义为;
元素字体中字形'0'(零,Unicode字符U + 0030)的宽度,或者更确切地说是提前量度。
这可以提供更准确的近似值40个字符;
静态演示:
p.block {
width: 40ch;
overflow-wrap: normal;
}
<p class="block">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.</p>
答案 1 :(得分:0)
虽然我会使用Lankymart的答案,如果唯一的问题是渲染文本,如果你真的需要“手动”分割文本,那么你可以使用类似的东西(编写的VBScript代码用cscript.exe
调用)
Option Explicit
Dim message
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."
WScript.Echo ".........1.........2.........3.........4.........5"
Dim line
For Each line In WordWrap( message, 40 )
WScript.Echo line
Next
Function WordWrap( inputText, lineLength ) ' as String()
Dim delimiter
delimiter = Chr(0) & Chr(0)
With New RegExp
.Pattern = "(.{1," & lineLength & "}|\S+?)(\s|$)"
.Global = True
WordWrap = .Replace( CStr(inputText), "$1" & delimiter )
WordWrap = Split( Left(WordWrap, Len(WordWrap) - Len(delimiter)), delimiter )
End With
End Function
这只是使用正则表达式搜索最合适的包络点,在找到的点放置分隔符并使用分隔符拆分文本。