我想将RichTextBox中每255个字符的文本拆分为vb 2010中的数组
示例是如果richtextbox有300个字符: array [0] =' 1 - 255 char array [1] =' 256 - 300 char
我是VB的新手,我找不到任何像样的链接或网站。 在此先感谢您的帮助。
答案 0 :(得分:0)
有很多方法可以做到这一点。就个人而言,我更喜欢将字符串拆分为集合 List(Of String)。它更容易搜索,您可以轻松地将其转换为数组。
'if you want just a collection List(Of String)
Dim resultList As List(Of String) = splitStringIntoList(RichTextBox.text, 255)
'Or if you want just a String array
Dim resultArray As String() = splitStringIntoList(RichTextBox.text, 255).ToArray
Public Function splitStringIntoList(ByVal instring As String, ByVal len As Integer) As List(Of String)
Dim splitlist As New List(Of String)
Do While instring.Length > 0
If instring.Length < len Then
'remaining string is too short, so shorten len
len = instring.Length
End If
splitlist.Add(instring.Substring(0, len))
instring = instring.Substring(len)
Loop
Return splitlist
End Function
答案 1 :(得分:0)
注释被评论以供参考:
Private Sub days_calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim s As String = rt.Text ' s holds the string input from the ritch text box
Dim totl_len = Len(s) ' is the variable to store the length of input string
Dim count As Integer = totl_len / 255 ' will count the array index(can be set to dynamic
Dim str_array(10) As String ' required to save the output array
Dim n As Integer = 254 ' limits the no. of characters per index is 255
Dim j As Integer = 0
For i As Integer = 0 To count - 1
If n < totl_len Then
n = 254
Else
n = totl_len
End If
str_array(i) = s.Substring(j, n).ToString 'Assign the values to the array
j = n
n = n + 254
Next
For i As Integer = 0 To count - 1 'display the content of the array
MsgBox(str_array(i).ToString())
Next
End Sub