Results 1 to 15 of 15

Thread: [RESOLVED] string into array based on length

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    47

    Resolved [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

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    47

    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?

  4. #4

  5. #5
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    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.

  6. #6
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: string into array based on length

    I may be missing something, but wouldnt this only work if you were splitting it up into single characters instead of small strings?

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: string into array based on length

    Quote Originally Posted by 03myersd View Post
    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.

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    47

    Re: string into array based on length

    Quote Originally Posted by 03myersd View Post
    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

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: string into array based on length

    which version of vb are you using? assuming vb2008 or 10, try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim str As String = "Visual Basic.Net"
    5.         Dim noChars As Integer = 5
    6.         Dim myArr(If(str.Length Mod noChars = 0, (str.Length \ noChars) - 1, (str.Length \ noChars))) As String
    7.         For x As Integer = 0 To myArr.GetUpperBound(0)
    8.             myArr(x) = str.Substring(x * noChars, If(x * noChars + noChars <= str.Length, noChars, str.Length - (x * noChars)))
    9.         Next
    10.     End Sub
    11.  
    12. End Class

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: string into array based on length

    I don't like to be left out, so here's my contribution:
    vb.net Code:
    1. Private Function GetStringArray(ByVal text As String, ByVal charCount As Integer) As String()
    2.     Dim textLength = text.Length
    3.     Dim startIndex = 0
    4.     Dim strings As New List(Of String)(CInt(Math.Ceiling(textLength / charCount)))
    5.  
    6.     Do While startIndex < textLength
    7.         strings.Add(text.Substring(startIndex, _
    8.                                    Math.Min(charCount, _
    9.                                             textLength - startIndex)))
    10.         startIndex += charCount
    11.     Loop
    12.  
    13.     Return strings.ToArray()
    14. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: string into array based on length

    Quote Originally Posted by jmcilhinney View Post
    I don't like to be left out, so here's my contribution:
    vb.net Code:
    1. Private Function GetStringArray(ByVal text As String, ByVal charCount As Integer) As String()
    2.     Dim textLength = text.Length
    3.     Dim startIndex = 0
    4.     Dim strings As New List(Of String)(CInt(Math.Ceiling(textLength / charCount)))
    5.  
    6.     Do While startIndex < textLength
    7.         strings.Add(text.Substring(startIndex, _
    8.                                    Math.Min(charCount, _
    9.                                             textLength - startIndex)))
    10.         startIndex += charCount
    11.     Loop
    12.  
    13.     Return strings.ToArray()
    14. End Function
    is that an error where you dimensioned the list(of string)?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: string into array based on length

    Quote Originally Posted by .paul. View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: string into array based on length

    yeah that's the part i meant. never seen that before...

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    47

    Re: string into array based on length

    Quote Originally Posted by techgnome View Post
    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

    Quote Originally Posted by .paul. View Post
    which version of vb are you using? assuming vb2008 or 10, try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim str As String = "Visual Basic.Net"
    5.         Dim noChars As Integer = 5
    6.         Dim myArr(If(str.Length Mod noChars = 0, (str.Length \ noChars) - 1, (str.Length \ noChars))) As String
    7.         For x As Integer = 0 To myArr.GetUpperBound(0)
    8.             myArr(x) = str.Substring(x * noChars, If(x * noChars + noChars <= str.Length, noChars, str.Length - (x * noChars)))
    9.         Next
    10.     End Sub
    11.  
    12. 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
  •  



Click Here to Expand Forum to Full Width