将大字符串变量拆分为行< = 70个字符 [英] Splitting a large string variable into lines <= 70 chars

查看:134
本文介绍了将大字符串变量拆分为行< = 70个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要能够将大字符串变量分成

行数组,每行不超过70个字符。


字符串变量是文本,所以如果你抓住我的漂移,我还会喜欢在一个单词的结尾处结束

的行。 br />

例如,我有一个包含文本的大字符串变量:

"我已经看到了你不会相信的东西。攻击舰着火了猎户座肩上的b
。我看着Tannhauser门上的月光下的C光束闪闪发光。所有这些时刻都将丢失,就像雨中的泪水一样。是时候死了。


现在,每行限制70个字符(行*必须*以

完成的单词结束),I希望这样看起来像这样:

我见过你不会相信的东西。在猎户座的肩膀上攻击船只

。我看着Tannhauser门的

月光下的C光束闪闪发光。所有这些时刻都将丢失,

就像雨中的泪水。是时候死了。


即分成一系列x行。


谢谢,


Daren

Hi,

I need to be able to split large string variables into an array of
lines, each line can be no longer than 70 chars.

The string variables are text, so I would additionally like the lines
to end at the end of a word, if you catch my drift.

For example, I have a large string variable containing the text:
"I''ve seen things you people wouldn''t believe. Attack ships on fire
off the Shoulder of Orion. I watched C-beams glistening in the
moonlight at the Tannhauser Gate. All these moments will be lost, like
tears in rain. Time to die."

Now, with a limit of 70 chars per line (and lines *must* end with a
completed word), I want this to appear like this:
"I''ve seen things you people wouldn''t believe. Attack ships on fire"
"off the Shoulder of Orion. I watched C-beams glistening in the"
"moonlight at the Tannhauser Gate. All these moments will be lost,"
"like tears in rain. Time to die."

ie, split into an array of x lines.

Thanks,

Daren

推荐答案

只是一些想法,我不知道只有他们的命令

存在。在

输入字符串中找到从右边开始的第一个空格。然后,您可以在此之前将所有内容作为第一个

字符串。之后的所有内容都成为新的输入字符串。重复

,直到输入字符串小于70.


或者通过输入字符串一次计算一个字符,直到达到70

保存你找到的最后一个空格的位置。


或者你可以用split命令拆分输入字符串,给它

空格分隔符。然后将单词堆叠成新的字符串检查

他们不会在每个字符串中添加超过70个。


达人写道:
Just some thoughts off hand and I don''t know the commands only that they
exist. Locate the first space from the right starting at 70 in the
input string. You then can take everything before that as the first
string. Everything after that becomes the new input string. Repeat
until the input string is less than 70.

Or count one char at a time through the input string until you get to 70
saving the position of the last space you find.

Or you might split the input string with the split command giving it the
space as the delimiter. then stack the words into new strings checking
that they don''t add to more than 70 in each string.

Daren wrote:


我需要能够将大的字符串变量分成一行
行,每行不超过70个字符。


例如,我有一个大字符串包含文字的变量:
我看到过你不会相信的事情。在猎户座的肩膀上攻击船只着火了。我在Tannhauser门的月光下看着C光束闪闪发光。所有这些时刻都会消失,就像雨中的泪水一样。是时候死了。

现在,每行限制70个字符(行*必须*以完成的单词结尾),我希望它看起来像这样:
我见过你们不会相信的事情。在猎户座肩上攻击船只
。我看着在Tannhauser门的
月光下闪烁的C光束。所有这些时刻都将丢失,
就像雨中的泪水。是时候死了。

即分成一系列x行。

谢谢,

达人
Hi,

I need to be able to split large string variables into an array of
lines, each line can be no longer than 70 chars.

The string variables are text, so I would additionally like the lines
to end at the end of a word, if you catch my drift.

For example, I have a large string variable containing the text:
"I''ve seen things you people wouldn''t believe. Attack ships on fire
off the Shoulder of Orion. I watched C-beams glistening in the
moonlight at the Tannhauser Gate. All these moments will be lost, like
tears in rain. Time to die."

Now, with a limit of 70 chars per line (and lines *must* end with a
completed word), I want this to appear like this:
"I''ve seen things you people wouldn''t believe. Attack ships on fire"
"off the Shoulder of Orion. I watched C-beams glistening in the"
"moonlight at the Tannhauser Gate. All these moments will be lost,"
"like tears in rain. Time to die."

ie, split into an array of x lines.

Thanks,

Daren


您正在寻找换行算法。我不知道是否有一个

内置功能为你做这个....这很有趣,而且很好的来自

尝试来吧在没有帮助的情况下独自完成这并不难。


基本上你将文本分成几个单词。用

单词填充一个字符串,直到你确定添加下一个单词将超过长度

限制,在你脑中打一个打字机DING声音,将字符串添加到你的

线阵,转到下一行。


-

-C。 Moya
www.cmoya.com

"达人" < SP **** @ gmail.com>在消息中写道

news:11 ********************** @ e56g2000cwe.googlegr oups.com ...
You''re looking for a line wrapping algorithm. I don''t know if there is a
built-in function to do it for you.... this is fun and a good excerise to
try and come up with on your own without help. It''s not hard.

Basically you split your text into an array of words. Fill a string with the
words until you determine that adding the next word would surpass the length
limit, make a typewriter DING sound in your head, add the string to your
lines array, move on to the next line.

--
-C. Moya
www.cmoya.com
"Daren" <sp****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...


我需要能够将大的字符串变量分成一行
行,每行不超过70个字符。


例如,我有一个大字符串包含文字的变量:
我看到过你不会相信的事情。在猎户座的肩膀上攻击船只着火了。我在Tannhauser门的月光下看着C光束闪闪发光。所有这些时刻都会消失,就像雨中的泪水一样。是时候死了。

现在,每行限制70个字符(行*必须*以完成的单词结尾),我希望它看起来像这样:
我见过你们不会相信的事情。在猎户座肩上攻击船只
。我看着在Tannhauser门的
月光下闪烁的C光束。所有这些时刻都将丢失,
就像雨中的泪水。是时候死了。

即分成一系列x行。

谢谢,

达人
Hi,

I need to be able to split large string variables into an array of
lines, each line can be no longer than 70 chars.

The string variables are text, so I would additionally like the lines
to end at the end of a word, if you catch my drift.

For example, I have a large string variable containing the text:
"I''ve seen things you people wouldn''t believe. Attack ships on fire
off the Shoulder of Orion. I watched C-beams glistening in the
moonlight at the Tannhauser Gate. All these moments will be lost, like
tears in rain. Time to die."

Now, with a limit of 70 chars per line (and lines *must* end with a
completed word), I want this to appear like this:
"I''ve seen things you people wouldn''t believe. Attack ships on fire"
"off the Shoulder of Orion. I watched C-beams glistening in the"
"moonlight at the Tannhauser Gate. All these moments will be lost,"
"like tears in rain. Time to die."

ie, split into an array of x lines.

Thanks,

Daren


嗨达人,


我尝试了一个CJ的优秀建议(实际上是第一个)

,它对我有用。这对我来说似乎是最有效的方法。

正如CMM所说,尝试自己是一项有趣的练习。


如果你' '仍然卡住,这里是代码。


(我使用的是更长的线长度65,因为我们需要搜索
$ b $在这个长度之后的空间。这意味着每条线的长度大约为65-75美元,平均而言,取决于最后一个字的大小。)


============================================

Dim LineLength As Integer = 50

Dim currPos As Integer

Dim theText As String =" My Large String goes here

将thisLine作为字符串调暗

将所有线条调暗为新的StringBuilder()

''在指定的编号后找到第一个空格。 chars。(LineLength)

而theText.Length> LineLength

''找到70个字符后的第一个空格。

currPos = theText.IndexOf(",LineLength)

如果currPos > -1然后

''从字符串开头到currPos获取所有文本

thisLine = theText.Substring(0,currPos + 1)

''也从原始字符串中删除这个提取的部分。

theText = theText.Remove(0,currPos + 1)

''将此行和CrLf附加到StringBuilder

allLines.Append(thisLine)

allLines.Append(vbCrLf)

结束如果

结束时

''将文本的剩余部分(最后一行)附加到StringBuilder

allLines.Append(theText)

''显示多行文本框中的文字

TextBox1.Text = allLines.ToString()


================ ============================

HTH,


问候,


Cerebrus。

Hi Daren,

I tried out one of CJ''s excellent suggestions(the first one, actually)
and it works for me. This seems to be the most efficient method to me.
As CMM said, it''s an interesting exercise to try out yourself.

If you''re still stuck, here''s the code.

(I''m using a preferable Line Length of 65, since we need to search for
a space after this length. This means that each line is about 65-75
chars in length, on average, depending on how big the last word is.)

============================================

Dim LineLength As Integer = 50
Dim currPos As Integer
Dim theText As String = "My Large String goes here"
Dim thisLine As String
Dim allLines As New StringBuilder()
''Locate the first space after specified no. of chars.(LineLength)
While theText.Length > LineLength
''Locate the first space after 70 chars.
currPos = theText.IndexOf(" ", LineLength)
If currPos > -1 Then
''Get all the text from start of string to currPos
thisLine = theText.Substring(0, currPos + 1)
''Remove this extracted part from the original string too.
theText = theText.Remove(0, currPos + 1)
''Append this line and a CrLf to the StringBuilder
allLines.Append(thisLine)
allLines.Append(vbCrLf)
End If
End While
''Append the remaining part of the text(last line) to the StringBuilder
allLines.Append(theText)
''Display the Text in a Multiline Textbox
TextBox1.Text = allLines.ToString()

============================================

HTH,

Regards,

Cerebrus.


这篇关于将大字符串变量拆分为行&lt; = 70个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆