Results 1 to 13 of 13

Thread: VB String Manipulation

  1. #1
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts

    VB String Manipulation

    Hi all,

    I'm looking to split a String in vb into its seperate characters and store the characters into seperate variables.

    ie split the string '4t6up'

    into the following

    letter1 = '4'
    letter2 = 't'
    letter3 = '6'
    letter4 = 'u'
    letter5 = 'p'

    I appreciate there is prob some easy way to do this lol, but any help would be great.

    Thanks

  2. #2
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    Not sure how correct the syntax is, but something along the lines of:

    Code:
    count = 0
    
    word = whatevervariableyourstringisin
    
    while (strlen(word) > 0)
         letter[count] = left(word,1)   <---- Can't remember how to do arrays
         word = right(word,(strlen(word) - 1))
    wend
    Hope that helps

    Mike.
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  3. #3
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks for the reply,

    I see where you're going with it, however 'letter[count]' when inputted into vb seperates the letter variable from count hence not producing letter1... letter2 etc.

    i'll keep playing around with it though

  4. #4
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    You can dynamically create variable names in VB?

  5. #5
    Senile Member
    Join Date
    Dec 2003
    Posts
    442
    Thanks
    3
    Thanked
    0 times in 0 posts
    Surely you want an array?

  6. #6
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    Apparently, you need normal brackets for an array rather than square ones (I assume having letter(1) is just as good as letter1). So this, with appropiate VB adjustments, such as Dim statements or whatever you're supposed to use to create variables, should work:

    Code:
    count = 0
    
    word = whatevervariableyourstringisin
    
    while (strlen(word) > 0)
         letter(count) = left(word,1)   <---- Can't remember how to do arrays
         word = right(word,(strlen(word) - 1))
    wend
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  7. #7
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks for help guys, still getting an error when trying to pass the letters into the array, I think VB thinks that its a function called letter of the format 'function(variables)' so still no joy.

  8. #8
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts
    Sorry my mistake, I didnt initialise the array (stupid mistake lol) ok so I think we're gettin there now, however I get an error saying 'subscript out of range' on the line starting 'letter(count)....

    Heres what I have



    Dim Count As Integer
    Dim letter() As String
    Count = 0

    word = Module1.user

    While (Len(word) > 0) ' Len used to get string length opposed to strlen
    letter(Count) = Left(word, 1)
    word = Right(word, (Len(word) - 1))
    Wend

  9. #9
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    What happens if you initialise the array with a size that you know is big enough e.g.

    Code:
    Dim letter(100) As String
    ?
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  10. #10
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts
    That was the problem lol, thanks for all your help mike_w, really appreciate it.

    just incase anyone in the future wants a working piece of code for this see below



    Dim Count As Integer
    Dim letter(100) As String

    Count = 1
    word = Module1.user

    While (Len(word) > 0)
    letter(Count) = Left(word, 1)
    word = Right(word, (Len(word) - 1))
    Count = Count + 1
    Wend

  11. #11
    Senile Member
    Join Date
    Dec 2003
    Posts
    442
    Thanks
    3
    Thanked
    0 times in 0 posts
    I can't remember much vb (so excuse any syntax errors ) but would this be a slightly more straightforward way of achieving what you want?

    Dim letter(len(Module1.user)) As String ' then you dont end up with lots of blanks and it can cope with >100

    for i=1 to len(word)
    letter(i)=midstr(Module1.user,i,1)
    next

  12. #12
    Member
    Join Date
    Jul 2003
    Location
    Shefflield (Currently at uni in staffs)
    Posts
    149
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks for your input RedPutty, I'll have a play with your suggestion

  13. #13
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,494
    Thanks
    335
    Thanked
    313 times in 249 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    or you could just use the built in Split() function/method whatever VB wants to call it.

    If you can use it or not depends on which version of VB you have though, eg 5/6/.Net/.Net2

    i *think* it is in 6 and upwards, although I have only used it in .net/.net2

    What it will do is take a string and split it up into an array of characters (or strings depending on how you use it) based on a separator. This can just be nothing - ie it splits at every char, or it can be another string or char. for example (from memory so don't complain if i get the syntax wrong, VB doesn't follow the rules).

    Code:
    Dim sStringToSplit As String = "This is your string"
    Dim sSplitArray() As String = Split(sStringToSplit)
    Pretty simple
    If you print out the array it should give you an output similar to this:
    Code:
    sSplitArray(0) = T
    sSplitArray(1) = H
    sSplitArray(2) = I
    sSplitArray(3) = S
    sSplitArray(4) = <blank>
    hope that helps

    etc etc

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Just got my first 12 string
    By Alex in forum Consumer Electronics
    Replies: 8
    Last Post: 10-07-2004, 01:00 PM
  2. Zakky - Thanks for the string dude :)
    By Tumble in forum General Discussion
    Replies: 3
    Last Post: 03-06-2004, 03:01 PM
  3. USA/Canada ONLY: Free VB .NET 2003 Standard Edition
    By Atomic in forum Retail Therapy and Bargains
    Replies: 2
    Last Post: 31-05-2004, 08:14 PM
  4. String and rope......
    By Zak33 in forum General Discussion
    Replies: 17
    Last Post: 31-01-2004, 03:52 PM
  5. passing a variable in VB?
    By streetster in forum Software
    Replies: 14
    Last Post: 02-01-2004, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •