I have a multiline textbox and I want to make an array of strings, However I would like to concatenate a new line when string has more than 60 chars.
我有一个多行文本框,我想创建一个字符串数组,但我想在字符串超过60个字符时连接一个新行。
So supposing I have a text:
假设我有一个文字:
A geologic period is one of several subdivisions of geologic time enabling cross-referencing of rocks and geologic events from place to place. These periods form elements of a hierarchy of divisions into which geologists have split the earth's history.
be converted to:
转换为:
A geologic period is one of several subdivisions of geologic \n
time enabling cross-referencing of rocks and geologic events \n
from place to place. These periods form elements of a \n
hierarchy of divisions into which geologists have split the \n
earth's history.
So to do so I was plannig to use
所以这样做是我计划使用的
array_strings = myMultilineText.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
and then loop through each string in array like
然后循环遍历数组中的每个字符串
For index = 0 To array_strings.GetUpperBound(0)
If array_strings(index).Length < 60 Then
MessageBox.Show(array_strings(index))
Else
'add new line...
End If
Next
Is there a better way to do this?
有一个更好的方法吗?
1 个解决方案
#1
4
If you want your text to be wrapped in a TextBox
you can just set the "WordWrap" property to True. But in case you want an algorithm you can use this code in c# (you can convert it to VB if you want, it's so simple)
如果希望将文本包装在TextBox中,只需将“WordWrap”属性设置为True即可。但是如果你想要一个算法,你可以在c#中使用这个代码(如果你愿意,可以将它转换为VB,就这么简单)
c# Code:
c#代码:
string longString = "Your long string goes here...";
int chunkSize = 60;
int chunks = longString.Length / chunkSize;
int remaining = longString.Length % chunkSize;
StringBuilder longStringBuilder = new StringBuilder();
int index;
for (index = 0; index < chunks * chunkSize; index += chunkSize)
{
longStringBuilder.Append(longString.Substring(index, chunkSize));
longStringBuilder.Append(Environment.NewLine);
}
if (remaining != 0)
{
longStringBuilder.Append(longString.Substring(index, remaining));
}
string result = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());
VB Code (Converted via developerfusion Conversion Tool):
VB代码(通过developerfusion转换工具转换):
Dim longString As String = "Your long string goes here..."
Dim chunkSize As Integer = 60
Dim chunks As Integer = longString.Length \ chunkSize
Dim remaining As Integer = longString.Length Mod chunkSize
Dim longStringBuilder As New StringBuilder()
Dim index As Integer
index = 0
While index < chunks * chunkSize
longStringBuilder.Append(longString.Substring(index, chunkSize))
longStringBuilder.Append(Environment.NewLine)
index += chunkSize
End While
If remaining <> 0 Then
longStringBuilder.Append(longString.Substring(index, remaining))
End If
Dim result As String = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray())
#1
4
If you want your text to be wrapped in a TextBox
you can just set the "WordWrap" property to True. But in case you want an algorithm you can use this code in c# (you can convert it to VB if you want, it's so simple)
如果希望将文本包装在TextBox中,只需将“WordWrap”属性设置为True即可。但是如果你想要一个算法,你可以在c#中使用这个代码(如果你愿意,可以将它转换为VB,就这么简单)
c# Code:
c#代码:
string longString = "Your long string goes here...";
int chunkSize = 60;
int chunks = longString.Length / chunkSize;
int remaining = longString.Length % chunkSize;
StringBuilder longStringBuilder = new StringBuilder();
int index;
for (index = 0; index < chunks * chunkSize; index += chunkSize)
{
longStringBuilder.Append(longString.Substring(index, chunkSize));
longStringBuilder.Append(Environment.NewLine);
}
if (remaining != 0)
{
longStringBuilder.Append(longString.Substring(index, remaining));
}
string result = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());
VB Code (Converted via developerfusion Conversion Tool):
VB代码(通过developerfusion转换工具转换):
Dim longString As String = "Your long string goes here..."
Dim chunkSize As Integer = 60
Dim chunks As Integer = longString.Length \ chunkSize
Dim remaining As Integer = longString.Length Mod chunkSize
Dim longStringBuilder As New StringBuilder()
Dim index As Integer
index = 0
While index < chunks * chunkSize
longStringBuilder.Append(longString.Substring(index, chunkSize))
longStringBuilder.Append(Environment.NewLine)
index += chunkSize
End While
If remaining <> 0 Then
longStringBuilder.Append(longString.Substring(index, remaining))
End If
Dim result As String = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray())