Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Splitting a string into variables?


Reply
 
Thread Tools Display Modes
  #1  
Old 03-01-2003, 08:11 PM
AcidGoat
Guest
 
Posts: n/a
Default Splitting a string into variables?


Ok I found some threads that sort of touched on this, but none that really gave a straight answer. I have a string of characters and I want each of those characters to be a seperate variable. For example, say I have a string that says "Bob". I want one variable for "B", one for "o" and one for "b" So that I can deal with each seperately.

Thanks in advance

--AcidGoat
Reply With Quote
  #2  
Old 03-01-2003, 08:17 PM
Kaluriel's Avatar
Kaluriel Kaluriel is offline
Senior Contributor
 
Join Date: Jun 2002
Location: Where Heaven & Hell meet
Posts: 813
Default

Ostr is the old string
Nstr is the new string

Code:
ReDim Nstr(Len(Ostr))
For i = 1 To Len(Ostr)
    Nstr(i) = Mid(Ostr, i, 1)
Next i
__________________
"They say the the world will end in the same way it was created. I disagree, I think it'll end without the internet" ~ Kaluriel 2001
"The edit button is mightier than the doublepost" ~ Kaluriel 2003
Reply With Quote
  #3  
Old 03-01-2003, 08:21 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

Or..

Code:
Dim b() As Byte
b = StrConv(OStr, vbFromUnicode)
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #4  
Old 03-01-2003, 08:23 PM
Kaluriel's Avatar
Kaluriel Kaluriel is offline
Senior Contributor
 
Join Date: Jun 2002
Location: Where Heaven & Hell meet
Posts: 813
Default

**** you, your always one step ahead lol
__________________
"They say the the world will end in the same way it was created. I disagree, I think it'll end without the internet" ~ Kaluriel 2001
"The edit button is mightier than the doublepost" ~ Kaluriel 2003
Reply With Quote
  #5  
Old 03-01-2003, 08:29 PM
AcidGoat
Guest
 
Posts: n/a
Default

Hmm... maybe I should have mentioned this: the string will always be 12 characters long. I need each character to be its own variable, appropriately named. The first character will be a variable called "char1" the second "char2" etc... Example: say the string is "goatgoatgoat" (excuse the lack of creativity ). I want this to result: char1 = g, char2 = o, char3 = a, char4 = t, char5 = g, char6 = o, char7 = a, char8 = t, char9 = g, char10 = o, char11 = a, char12 = t.

Is this even possible?
Reply With Quote
  #6  
Old 03-01-2003, 08:41 PM
Machaira's Avatar
Machaira Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

Sure, but it'll be ugly.
Code:
Dim char1 As String Dim char2 As String Dim char3 As String Dim char4 As String Dim char5 As String Dim char6 As String Dim char7 As String Dim char8 As String Dim char9 As String Dim char10 As String Dim char11 As String Dim char12 As String Dim sString As String Dim iLp As Integer sString = "goatgoatgoat" Dim sChar As String For iLp = 0 To 11 sChar = Mid$(sString, iLp, 1) Select Case iLp Case 0 char1 = sChar Case 1 char2 = sChar Case 2 char3 = sChar Case 3 char4 = sChar Case 4 char5 = sChar Case 5 char6 = sChar Case 6 char7 = sChar Case 7 char8 = sChar Case 8 char9 = sChar Case 9 char10 = sChar Case 10 char11 = sChar Case 11 char12 = sChar End Select Next iLp
Reply With Quote
  #7  
Old 03-01-2003, 08:50 PM
AcidGoat
Guest
 
Posts: n/a
Default

Hmmm... I tried that code and got this: Run-time error '5': Invalid procedure call or argument. Upon hitting debug, this line was highlighted: sChar = Mid$(sString, iLp, 1)

Maybe there's somethign I'm missing, but I've never seen code like this before (I haven't been using VB for very long, sorry).
Reply With Quote
  #8  
Old 03-01-2003, 09:16 PM
AcidGoat
Guest
 
Posts: n/a
Default

After a bit of tinkering and investigating I was able to get it to work. This is what I used:

sString = "goatgoatgoat"

char1 = Mid$(sString, 1, 1)
char2 = Mid$(sString, 2, 1)
char3 = Mid$(sString, 3, 1)
char4 = Mid$(sString, 4, 1)
char5 = Mid$(sString, 5, 1)
char6 = Mid$(sString, 6, 1)
char7 = Mid$(sString, 7, 1)
char8 = Mid$(sString, 8, 1)
char9 = Mid$(sString, 9, 1)
char10 = Mid$(sString, 10, 1)
char11 = Mid$(sString, 11, 1)
char12 = Mid$(sString, 12, 1)
Reply With Quote
  #9  
Old 03-01-2003, 09:16 PM
Kaluriel's Avatar
Kaluriel Kaluriel is offline
Senior Contributor
 
Join Date: Jun 2002
Location: Where Heaven & Hell meet
Posts: 813
Default

iLp could be 1 to 12. You can't start a character number 0
__________________
"They say the the world will end in the same way it was created. I disagree, I think it'll end without the internet" ~ Kaluriel 2001
"The edit button is mightier than the doublepost" ~ Kaluriel 2003
Reply With Quote
  #10  
Old 03-01-2003, 11:30 PM
diamond_panther's Avatar
diamond_panther diamond_panther is offline
Junior Contributor
 
Join Date: Oct 2002
Location: The Moon - Yes! Its true
Posts: 222
Default

why do u want each letter to have its own variable?
isn't this EXACTLY what an array is for?
__________________
"...You would know zat another word for an iconographer would be 'photographer'? From the old word photus in Latation, vhich means-'
'To prance around like a pillock ordering everyone about as if you owned the place?..."
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a file in dat question ? murphys General 5 11-06-2002 08:34 PM
Open an associated Text Document knoxitis General 19 05-01-2002 11:26 PM
Shareware Registry Protection Technigue karachi999 General 2 01-21-2002 02:40 PM
Loop to format file kingesk General 2 09-10-2000 05:01 PM
File format problem kingesk General 1 09-08-2000 04:52 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->