|
-
Oct 26th, 2004, 11:17 AM
#1
String Functions??
Simple problem: Two text boxes, conveniently labled txtText and txtCode. Two command buttons, labled cmdEncode and cmdDecode.
Take a guess what I need. I don't konw enough about string functions, but I need it to go through the string and replace "a" with "1-", "b" with "2-", and so on and so forth. Shouldn't be hard, but I haven't programmed in months and I'm a little rusty..
Last edited by timeshifter; Oct 27th, 2004 at 11:56 AM.
-
Oct 26th, 2004, 11:18 AM
#2
Isn't there a Replace function?
My usual boring signature: Nothing
 
-
Oct 26th, 2004, 11:20 AM
#3
Im a little Chris.
Replace(stringofdata, data to replace, what to replace it with)
-
Oct 26th, 2004, 11:22 AM
#4
I'm assuming your "data to replace" and "data to replace with" are both strings in quotes?
-
Oct 26th, 2004, 11:25 AM
#5
-
Oct 26th, 2004, 11:25 AM
#6
Lively Member
youre right, or a string variable.
Last edited by Xcoder : 09-10-2001 at 12:45 AM.
-
Oct 26th, 2004, 11:29 AM
#7
Too many variables in the replace function.. what do they all mean?
-
Oct 26th, 2004, 12:16 PM
#8
Lively Member
Here's how I undid it.....
VB Code:
Private Sub cmdDecode_Click()
Dim myCode As String
Dim CodeChar As String
Dim intCount As Integer
Dim Needle As String
Dim intLetterNum As Integer
myCode = txtCode.Text
intLetterNum = Asc("Z")
For intCount = 26 To 1 Step -1
CodeChar = Chr$(intLetterNum) 'This is what the "code" will be (letters Z through A)
Needle = CStr(intCount) & "-" 'Needle is the item being looked for... a number followed by a dash.
myCode = Replace(myCode, Needle, CodeChar) 'In MyCode, replace all ocurrances of Needle with CodeChar
Needle = CStr(intCount) & " " ' Not all numbers were followed by a dash, some had a space.
myCode = Replace(myCode, Needle, CodeChar & " ")
Needle = CStr(intCount) & "." 'While others were followed by a period
myCode = Replace(myCode, Needle, CodeChar & ".")
intLetterNum = intLetterNum - 1 'Back up one letter; Z, y, X.... C, B, A
Next
txtText.Text = myCode
End Sub
Ok, now for the why I went Z to A instead of the other way around.
Simple.
Consider this: 1-14-4-
1- = A
14- = N
4- = D
If I replace all 1- with "A", I get A14-4-
If I were to then replace 4- with "D", I'd get A1DD..... which isn't right.
But in working in reverse:
IF I replace 14- with "N" I'm left with 1-N4-
I can then safely (and correctly) replace 4- with "D" to get 1-ND
And finally replacing 1- with "A" gives me the final AND.
-
Oct 27th, 2004, 08:01 AM
#9
I'm still having problems getting it to actually encode it... There is a replace function, but I dont' know what numbers I need in the command for it to register. It keeps telling me I'm missing something, but I can't figure out where.
-
Oct 27th, 2004, 08:06 AM
#10
Run em backwards from highest to lowest
14-
4-
1-
That way, 14- was already replaced by a char so the error on 4- is avoided with Replace()
-
Oct 27th, 2004, 08:09 AM
#11
I still can't get the Replace() function to work. That's my big question. Once I get that, I can solve the rest of it. The only problem is that I can't figure out the syntax for the function.
-
Oct 27th, 2004, 08:10 AM
#12
And unless you provide sample of the raw, encoded and decoded strings, we can only guess what your trying to achieve.
Someone mentioned spaces and periods were also used.
-
Oct 27th, 2004, 08:13 AM
#13
All I need is the syntax to make Replace() work. When that's done, I'll put up the entire program so you can see how I did it. Syntax is all I need right now, and the VB Index isn't being that helpful.
-
Oct 27th, 2004, 08:14 AM
#14
MSDN
Replace(expression, find, replacewith[, start[, count[, compare]]])
expression Required.String expression containing substring to replace.
find Required. Substring being searched for.
replacewith Required. Replacement substring.
start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed.
count Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.
compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.
Settings
vbUseCompareOption or –1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare or 0 Performs a binary comparison.
vbTextCompare or 1 Performs a textual comparison.
vbDatabaseCompare or 2 Microsoft Access only. Performs a comparison based on information in your database.
-
Oct 27th, 2004, 08:16 AM
#15
Now make it fully work without VB telling you that you are missing = somewhere.
-
Oct 27th, 2004, 08:20 AM
#16
-
Oct 27th, 2004, 08:53 AM
#17
Lively Member
Originally posted by timeshifter
Now make it fully work without VB telling you that you are missing = somewhere.
Dude, replace is a function....
varable = Replace(haystack, needle, replaceWith)
Simple.... did you not look at the code that I posted? It clearly used the replace function.
haystack = the variable/string you want to search in
needle = the variable/string you are looking for
replaceWith = the variable/string to replace your needle with when needle is found in haystack.
-
Oct 27th, 2004, 11:50 AM
#18
Sorry, brain freeze. Haven't programmed in a while, and I've forgotten some of the stuff. My apologies, Talon.
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
|