|
-
Jul 20th, 2010, 02:50 PM
#1
Thread Starter
Member
[RESOLVED] string into array based on length
Hi can anyone tell me how to split a string into an array based on it's length?
Say if I want the length to be 350 then every 350 chars in a textbox will be split into an array like
splittext(0) = First 350 chars
splittext(1) = Second 350 chars
splittext(2) = Third 350 chars or any chars under 350
etcetc
Thanks
-
Jul 20th, 2010, 03:06 PM
#2
Frenzied Member
Re: string into array based on length
Try this:
Code:
Dim str As String = "hello world!"
Dim noChars As Integer = 4
Dim myArr(str.Length \ noChars) As String
For i As Integer = 1 To str.Length \ noChars
myArr(i) = Mid(str, (noChars * i) - (noChars - 1), noChars)
MsgBox(myArr(i))
Next
-
Jul 20th, 2010, 03:14 PM
#3
Thread Starter
Member
Re: string into array based on length
Thanks works great but is there any way to make it so that it can be any number below noChars?
So like
Code:
Dim str As String = "Visual"
it gets the first 4 letters into an array and then puts the 2 left over letters into the array aswell?
-
Jul 20th, 2010, 03:19 PM
#4
Re: string into array based on length
-
Jul 20th, 2010, 03:21 PM
#5
Frenzied Member
Re: string into array based on length
Code:
Dim str As String = "hello"
Dim noChars As Integer = 6
Dim myArr(str.Length \ noChars) As String
If noChars <= str.Length Then
For i As Integer = 1 To str.Length \ noChars
myArr(i) = Mid(str, (noChars * i) - (noChars - 1), noChars)
MsgBox(myArr(i))
Next
Else
ReDim myArr(1)
myArr(0) = str
MsgBox(myArr(0))
End If
That fixes it for if noChars is greater than the length of the string.
-
Jul 20th, 2010, 03:24 PM
#6
Frenzied Member
Re: string into array based on length
 Originally Posted by gep13
I may be missing something, but wouldnt this only work if you were splitting it up into single characters instead of small strings?
-
Jul 20th, 2010, 03:26 PM
#7
Re: string into array based on length
 Originally Posted by 03myersd
I may be missing something, but wouldnt this only work if you were splitting it up into single characters instead of small strings?
Ah, my bad, I misread the first post.
-
Jul 20th, 2010, 03:45 PM
#8
Thread Starter
Member
Re: string into array based on length
 Originally Posted by 03myersd
Code:
Dim str As String = "hello"
Dim noChars As Integer = 6
Dim myArr(str.Length \ noChars) As String
If noChars <= str.Length Then
For i As Integer = 1 To str.Length \ noChars
myArr(i) = Mid(str, (noChars * i) - (noChars - 1), noChars)
MsgBox(myArr(i))
Next
Else
ReDim myArr(1)
myArr(0) = str
MsgBox(myArr(0))
End If
That fixes it for if noChars is greater than the length of the string.
Almost what I wanted! Sorry I put the last request wrong 
I meant put the leftovers into a new array
Like
myArr(0) = first 6 chars
myarr(1) = last 2 chars
-
Jul 20th, 2010, 04:04 PM
#9
Re: string into array based on length
Here's what I came up with... subtle differences:
Code:
Dim str As String = "Visual"
Dim noChars As Integer = 4
Dim myArr(str.Length \ noChars) As String
If noChars <= str.Length Then
For i As Integer = 1 To myArr.Length
myArr(i - 1) = Mid(str, (noChars * i) - (noChars - 1), noChars)
MsgBox(myArr(i - 1))
Next
Else
ReDim myArr(1)
myArr(0) = str
MsgBox(myArr(0))
End If
-tg
-
Jul 20th, 2010, 11:44 PM
#10
Re: string into array based on length
which version of vb are you using? assuming vb2008 or 10, try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String = "Visual Basic.Net"
Dim noChars As Integer = 5
Dim myArr(If(str.Length Mod noChars = 0, (str.Length \ noChars) - 1, (str.Length \ noChars))) As String
For x As Integer = 0 To myArr.GetUpperBound(0)
myArr(x) = str.Substring(x * noChars, If(x * noChars + noChars <= str.Length, noChars, str.Length - (x * noChars)))
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2010, 12:13 AM
#11
Re: string into array based on length
I don't like to be left out, so here's my contribution:
vb.net Code:
Private Function GetStringArray(ByVal text As String, ByVal charCount As Integer) As String() Dim textLength = text.Length Dim startIndex = 0 Dim strings As New List(Of String)(CInt(Math.Ceiling(textLength / charCount))) Do While startIndex < textLength strings.Add(text.Substring(startIndex, _ Math.Min(charCount, _ textLength - startIndex))) startIndex += charCount Loop Return strings.ToArray() End Function
-
Jul 21st, 2010, 12:22 AM
#12
Re: string into array based on length
 Originally Posted by jmcilhinney
I don't like to be left out, so here's my contribution:
vb.net Code:
Private Function GetStringArray(ByVal text As String, ByVal charCount As Integer) As String()
Dim textLength = text.Length
Dim startIndex = 0
Dim strings As New List(Of String)(CInt(Math.Ceiling(textLength / charCount)))
Do While startIndex < textLength
strings.Add(text.Substring(startIndex, _
Math.Min(charCount, _
textLength - startIndex)))
startIndex += charCount
Loop
Return strings.ToArray()
End Function
is that an error where you dimensioned the list(of string)?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2010, 12:30 AM
#13
Re: string into array based on length
 Originally Posted by .paul.
is that an error where you dimensioned the list(of string)?
Do you mean this part:
Code:
Dim strings As New List(Of String)(CInt(Math.Ceiling(textLength / charCount)))
One of the List constructors takes a capacity as an argument, and that's the one I'm using. The Capacity of a List is the number of elements it can hold, which is determined by the Length of the internal array. If you don't specify a capacity then the internal array is initially created with four elements. Each time you Add an item that takes the Count past the current Capacity, the internal array must be resized; specifically, the Length is doubled. If you know exactly how many items the List will hold, as we do in this case, you can specify the capacity when you create it and you guarantee that no array resizing will ever take place, increasing efficiency.
-
Jul 21st, 2010, 12:43 AM
#14
Re: string into array based on length
yeah that's the part i meant. never seen that before...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2010, 09:01 AM
#15
Thread Starter
Member
Re: string into array based on length
 Originally Posted by techgnome
Here's what I came up with... subtle differences:
Code:
Dim str As String = "Visual"
Dim noChars As Integer = 4
Dim myArr(str.Length \ noChars) As String
If noChars <= str.Length Then
For i As Integer = 1 To myArr.Length
myArr(i - 1) = Mid(str, (noChars * i) - (noChars - 1), noChars)
MsgBox(myArr(i - 1))
Next
Else
ReDim myArr(1)
myArr(0) = str
MsgBox(myArr(0))
End If
-tg
Using a 4 letter word with that made a blank msgbox appear after the first
 Originally Posted by .paul.
which version of vb are you using? assuming vb2008 or 10, try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String = "Visual Basic.Net"
Dim noChars As Integer = 5
Dim myArr(If(str.Length Mod noChars = 0, (str.Length \ noChars) - 1, (str.Length \ noChars))) As String
For x As Integer = 0 To myArr.GetUpperBound(0)
myArr(x) = str.Substring(x * noChars, If(x * noChars + noChars <= str.Length, noChars, str.Length - (x * noChars)))
Next
End Sub
End Class
Thanks! Works perfect :P
Thanks for the help guys, much appreciated
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|