[RESOLVED] Grabbing consecutive parts of a string of unknown length
Hi all,
I'm using VB6, just wondering how to grab characters consecutively out of a string (of unknown length), for modification?
For example, this string must be at least 5 characters long and less than 100.
So, a textbox takes in the string. I then want to pick each part in sequence and act on it, e.g.
String1.char = keyascii+1
loop (until end of string)
Do something else
So the string
ABCDE
in this example would become
BCDEF
Just as example. I'm sure there's an easy way to do it but I'm just not sure how - any ideas?
Thanks,
Arby.
p.s. I'm not asking how to change the keyascii, just the part about grabbing bits of the string :)
Re: Grabbing consecutive parts of a string of unknown length
well here's one idea (using you example)
Code:
Dim ExampleString As String
Dim StringBuf() As Byte
Dim i As Long
ExampleString = "This is a string I want to play with"
StringBuf = ExampleString
For i = 0 To UBound(StringBuf) Step 2
StringBuf(i) = StringBuf(i) + 1
Next i
Debug.Print StringBuf
Re: Grabbing consecutive parts of a string of unknown length
This works:
Code:
Dim MyData As String
Private Sub Form_Load()
MyData = "ABCDEFGHIJKLMNOPQR"
For I = 1 To Len(MyData)
If Asc(Mid(MyData, I, 1)) < 255 Then
MyData = Left(MyData, I - 1) + Chr(Asc(Mid(MyData, I, 1)) + 1) + Mid(MyData, I + 1)
End If
Next
MsgBox MyData ' Returns BCDEFGHIJKLMNOPQRS
End Sub
Re: Grabbing consecutive parts of a string of unknown length
Quote:
Originally Posted by Milk
well here's one idea (using you example)
Code:
Dim ExampleString As String
Dim StringBuf() As Byte
Dim i As Long
ExampleString = "This is a string I want to play with"
StringBuf = ExampleString
For i = 0 To UBound(StringBuf) Step 2
StringBuf(i) = StringBuf(i) + 1
Next i
Debug.Print StringBuf
Ah, thanks for that. However, I don't really understand how it works.
Could you maybe explain what "UBound" is, and what "i" is? I don't get the "For i = 0 to <something> step 2" bit but I've seen it before and I think that might be what I need. the example given is great but it isn't what I'm actually doing with the string, and I can't implement it myself if I don't understand it XD
Thanks!
Code Doc - Thanks for your reply too! Looks complex though :o
Re: Grabbing consecutive parts of a string of unknown length
For example, the following psuedocode (based on your example):
Code:
Private Sub Command2_Click()
Dim ExampleString As String
Dim StringBuf() As Byte
Dim i As Long
ExampleString = Text1.Text
StringBuf = ExampleString
For i = 0 To UBound(StringBuf) Step 2
If StringBuf(i) = "A" Then
StringBuf(i) = "1"
End If
Next i
Text2.Text = StringBuf
End Sub
How could I make this work?
Thanks! :)
Re: Grabbing consecutive parts of a string of unknown length
actually Code Doc's version is perhaps simpler because it deals with the string as a string, I'm dealing with it as a byte array, but to use your second example.
Code:
Dim ExampleString As String
Dim StringBuf() As Byte
Dim i As Long
ExampleString = "Aardvark"
StringBuf = ExampleString
For i = 0 To UBound(StringBuf) Step 2
'Ubound means the upper boundry of the Array,
'in this case it is the same as LenB(ExampleString)
'"Step 2" because, if dealing with an Ansi string every other
'byte is irrelivant because it is not a two byte unicode
'character, but a single byte Ansi Character (in a Unicode
'structure)
If StringBuf(i) = 65 Then 'AscW("A") = 65
StringBuf(i) = 49 ' AscW("1") = 49
End If
Next i
Text2.Text = StringBuf
Re: Grabbing consecutive parts of a string of unknown length
Ah, that's cool, thank you :) Complex though.
Would it be possible to ask for a breakdown of of second example using Code Doc's logic?
Sadly my knowledge of VB is very basic - all I know is the very basics of the language in quite a basic (generic) computing course a couple of years ago, which is a pain because I actually like writing programs, I just don't really know the language XD. All I'm saying is that I'm a complete noob, so you have to talk in layman's terms :D
Thanks again!
Re: Grabbing consecutive parts of a string of unknown length
Phew! Right...
Code doc is using the string functions Left$, Mid$ and in the same bag there is also Right$. Rather obviously perhaps, Left$ will return the left part of a string to a specified length. Mid$ and Right$ work similarly. An alternative is to copy the string to a byte array and work with the numbers that represent the letters rather than the letters. Which method is better very much depends on what you want to do.
What do you want to do?
Re: Grabbing consecutive parts of a string of unknown length
Fair enough ^^.
Ok, I want to do something like the following, where Text1 is a user input box (up to 100 chars length)
Code:
Dim Str1 as string
Str1 = Text1.text
Start loop
take one character
if character = A then
Character = 492
elseif character = B then
Character = 306
elseif character = C then
Character = 712
<etc>
end if
Loop: Go to next character
When finished, end loop
:)
Edit:
Maybe output those changed characters into a new string, too. So the string "AABC" would become "492492306712". Well, I want to add them together actually, but I guess that's irrelevent.
Re: Grabbing consecutive parts of a string of unknown length
Code:
Dim Str1 as string
Dim NewStr as string
Str1 = Text1.text
Start loop
take one character
if character = A then
NewStr = NewStr + 492
elseif character = B then
NewStr = NewStr + 306
elseif character = C then
NewStr = NewStr + 712
<etc>
end if
Loop: Go to next character
When finished, end loop
A re-do of the last one, with the extra variable.
Also, it would be handy to know how to do a bit-shift on the string (as in both of your original examples), but by a user-specified bit shift number. I.e. instead of shifting by one, it shifts each letter by a specified number of places (a kind of ceaser cipher).
Re: Grabbing consecutive parts of a string of unknown length
Re: Grabbing consecutive parts of a string of unknown length
If your converting characters to numbers than you can use replace() which will replace all instances of a letter, e.g.
yourString = Replace(yourString, "A", "492")
Re: Grabbing consecutive parts of a string of unknown length
Oh, that looks interesting! Thanks :)
Re: Grabbing consecutive parts of a string of unknown length
Quote:
Originally Posted by Arby
Well, I want to add them together actually, but I guess that's irrelevent.
...actually I think it might be quite valid (perhaps?)
You are assigning a number to characters, is this every character or just some?
Are you doing this a lot, or just a few times?
You want to add these numbers together?
Re: [RESOLVED] Grabbing consecutive parts of a string of unknown length
Basically, the user inputs a string. The program will convert each of the characters in the string, one by one, into a hard-coded number and add them to the total. It's the first step of a hashing kind of program ^^.
Re: [RESOLVED] Grabbing consecutive parts of a string of unknown length
Here's some more ideas for you... The Form_Load event contains 3 examples, only one would be needed. It's for a form with two Textboxes and and a command button. I hope you can make some sense of it.
Code:
Option Explicit
Dim CharNumbers(255) As Integer
Private Sub Form_Load()
Dim i As Long, ii As Long, Tmp As Integer
'Use Randomize 'Seed' to create a random sequence that
'will be the same every time the code is run.
'To change the sequence, change the seed
Randomize 666
'1_________________________________________
'For a sequence like...
'23,64,254,78,96,149,201... (255 values; min = 0, max = 255)
'Fill the array with the numbers 0 to 255
'i.e. CharNumbers(13) = 13
For i = 0 To 255
CharNumbers(i) = i
Next i
'Then swap these around randomly to get a
'random none repeating sequence
For i = 0 To 255
ii = Int(Rnd * 256)
Tmp = CharNumbers(i)
CharNumbers(i) = CharNumbers(ii)
CharNumbers(ii) = Tmp
Next i
'2_________________________________________
'For a sequence like...
'234,648,2542,782,964,14945,2014... (255 values; min = 0, max ~ 255*64)
'Fill the array with a larger range
'by adding with a random increment
'again no two numbers will be the same.
ii = 0
For i = 0 To 255
ii = ii + Int(Rnd * 128)
CharNumbers(i) = ii
Next i
'Then swap these around as before
For i = 0 To 255
ii = Int(Rnd * 256)
Tmp = CharNumbers(i)
CharNumbers(i) = CharNumbers(ii)
CharNumbers(ii) = Tmp
Next i
'3_________________________________________
'For a sequence you can completely control
'Assign a numerical value to each letter.
CharNumbers(0) = 498
CharNumbers(1) = 326
CharNumbers(2) = 128
CharNumbers(3) = 587
CharNumbers(4) = 192
'...some 249 lines later
CharNumbers(253) = 87
CharNumbers(254) = 145
CharNumbers(255) = 364
End Sub
Private Sub Command1_Click()
Dim buf() As Byte, i As Long
Dim Total As Long
buf = Text1.Text
For i = 0 To UBound(buf) Step 2
Total = (Total + CharNumbers(buf(i))) Mod 1000000 'arbitary number to roll over
Next i
Text2.Text = Total
End Sub
Re: [RESOLVED] Grabbing consecutive parts of a string of unknown length
Mm, looks interesting! Thanks, I'll look into that :).
Thanks again!