|
-
Nov 6th, 2002, 10:01 PM
#1
Thread Starter
Lively Member
Im lost... I cant figure out why this doesnt work
this code will repeat whatever youve typed in. but i want it to replace any "two" in the text with a 2. I thought this would work but it doesnt and i have no idea why, so if anyone knows I would much apreciate an answer.
Dim num(5020) As String
Private Sub Command1_Click()
For syl = 1 To 5000
num(syl) = Mid$(Text1.Text, (syl), 1)
Next syl
For spf = 1 To 5000
Select Case num(spf)
Case num(spf) = "t" And num(spf + 1) = "w" And num(spf + 2) = "o"
num(spf) = "2"
num(spf + 1) = ""
num(spf + 2) = ""
End Select
Next spf
For rite = 1 To 5000
Label1.Caption = Label1.Caption & num(rite)
Next rite
End Sub
-
Nov 6th, 2002, 10:08 PM
#2
Do you have to do it that way?
If not, maybe you can use the Replace function, like:
VB Code:
sBuff = Replace(sBuff, "two", " 2 ", , vbTextCompare)
Last edited by Bruce Fox; Nov 6th, 2002 at 10:24 PM.
-
Nov 6th, 2002, 10:09 PM
#3
Off hand I don't see anything wrong, but you might try changing
num(syl) = Mid$(Text1.Text, (syl), 1)
to
num(syl) = Mid$(Text1.Text, syl, 1)
It would be better however to use the Replace function and look for " two " (note the space before and after the two) and replace it with " 2 ".
-
Nov 6th, 2002, 10:10 PM
#4
Stuck in the 80s
use the replace function:
VB Code:
Dim num(5020) As String
Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, "two", "2", , , vbTextCompare)
For syl = 1 To 5000
num(syl) = Mid$(Text1.Text, (syl), 1)
Next syl
For rite = 1 To 5000
Label1.Caption = Label1.Caption & num(rite)
Next rite
End Sub
Edit: Man, beaten again
-
Nov 6th, 2002, 10:11 PM
#5
Thread Starter
Lively Member
Oh
i didnt know about the replace thing becouse im a newb so im always improvising, but the replace thing looks much easier so now i just gotta figure out all about that.
-
Nov 6th, 2002, 10:12 PM
#6
I just saw that I got beaten to the puch on my Replace suggestion, but it's better to look for " two " than "two" because with the latter you would change "two-by-four" to "2-by-four" and you probably don't want that.
-
Nov 6th, 2002, 10:14 PM
#7
Stuck in the 80s
Originally posted by MartinLiss
I just saw that I got beaten to the puch on my Replace suggestion, but it's better to look for " two " than "two" because with the latter you would change "two-by-four" to "2-by-four" and you probably don't want that.
Then you need to account for .'s and ,'s, etc. It could be a mess.
-
Nov 6th, 2002, 10:14 PM
#8
Thread Starter
Lively Member
ok
thx, do you know were i can find a tutorial about that command or should i just experiment with it?
-
Nov 6th, 2002, 10:16 PM
#9
And................ the problem was with the CASE statement.
It never occured.
Although this works, it isn't the way to go:
VB Code:
For spf = 1 To 5000
Select Case [b](num(spf) = "t" And num(spf + 1) = "w" And num(spf + 2) = "o")[/b]
Case True
num(spf) = "2"
num(spf + 1) = ""
num(spf + 2) = ""
End Select
Next spf
As I'm guessing ur going to do more letter matches.
As I mentioned (and others) use the Replace function.
-
Nov 6th, 2002, 10:19 PM
#10
Thread Starter
Lively Member
AHA!
no wonder! o well, now i need to know what vbTextCompare does.
-
Nov 6th, 2002, 10:19 PM
#11
Type the word Replace into you IDE, highlight it, and press F1.
That will give you the MSDN Help on Replace....
Note: Replace is applicable only to VB6
Also,
I extracted this from VBA.
Description
Replace
Returns a string in which a specified substring has been replaced with another substring a specified number of times.
Syntax
Replace(expression, find, replace[, start[, count[, compare]]])
The Replace function syntax has these named arguments:
Part Description
expression Required. String expression containing substring to replace.
find Required. Substring being searched for.
replace 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
The compare argument can have the following values:
Constant Value Description
vbUseCompareOption –1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.
Return Values
Replace returns the following values:
If Replace returns
expression is zero-length Zero-length string ("")
expression is Null An error.
find is zero-length Copy of expression.
replace is zero-length Copy of expression with all occurences of find removed.
start > Len(expression) Zero-length string.
count is 0 Copy of expression.
Remarks
The return value of the Replace function is a string, with substitutions made, that begins at the position specified by start and and concludes at the end of the expression string. It is not a copy of the original string from start to finish.
-
Nov 6th, 2002, 10:24 PM
#12
Thread Starter
Lively Member
thx
i dont have the msdn part so i cant use that but what you gave me was enough.
-
Nov 6th, 2002, 10:26 PM
#13
Re: thx
Originally posted by original_sneaky
i dont have the msdn part ...........
Do you M$ Word, Access or Excell?
If so, you can do the same (F1) thing in VBA.
-
Nov 6th, 2002, 10:30 PM
#14
Thread Starter
Lively Member
Oh
some people learn something new every day.
i learn a million things!
-
Nov 6th, 2002, 10:34 PM
#15
Thread Starter
Lively Member
if this works:
Dim num(5020) As String
Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, "two", "2", , , vbTextCompare)
For syl = 1 To 5000
num(syl) = Mid$(Text1.Text, (syl), 1)
Next syl
For rite = 1 To 5000
Label1.Caption = Label1.Caption & num(rite)
Next rite
End Sub
than shouldnt this work? all i do is add spaces before and after the two and 2, or do i have to give starting locations and ending ones now?:
Dim num(5020) As String
Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, " two ", " 2 ", , , vbTextCompare)
For syl = 1 To 5000
num(syl) = Mid$(Text1.Text, (syl), 1)
Next syl
For rite = 1 To 5000
Label1.Caption = Label1.Caption & num(rite)
Next rite
End Sub
-
Nov 6th, 2002, 10:35 PM
#16
Stuck in the 80s
Some basics:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim strString As String
strString = "Hey guys!"
MsgBox strString
strString = Replace(strString, "guys", "dudes", , , vbTextCompare)
MsgBox strString
strString = Replace(strString, "hey", "Yo", , , vbTextCompare)
MsgBox strString
'notice this one will not replace it, since it's case sensitive
'when it lacks the vbTextCompare
strString = Replace(strString, "DUDES", "Fuzz")
MsgBox strString
End Sub
-
Nov 6th, 2002, 10:41 PM
#17
Thread Starter
Lively Member
999,982 things learned, 18 things to go.
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
|