关于vb For next的一些问题,请高手们帮忙,多谢。。。

sealife24

新手上路
注册
2005-10-23
消息
107
荣誉分数
0
声望点数
0
ask for replace the indicating word of the other indicating word,like:canadian
replace u for all a
result: cunudiun
What;s wrong with the code below?I can't figure it out..please help me,it doesn't work..

Private Sub cmdNewWord_Click()

strWord = txtText.Text
strLetter = txtFindLetter.Text
strReplaceLetter = txtReplaceWith.Text
intLength = Len(strWord)

For intCount = intPosition To intLength

intPosition = InStr(1, strWord, strLetter)
'get the number of the position of the Finding letter
Mid(strWord, intPosition, 1) = strReplaceLetter
'The replace letter will replace the finding word
lblNewWord.Caption = strWord

Next intCount
End Sub
 
http://www.w3schools.com//func_instr.asp

InStr([start,]string1,string2[,compare])
InStr(1, strWord, strLetter)
////////////
The InStr function returns the position of the first occurrence of one string within another.

The InStr function can return the following values:

* If string1 is "" - InStr returns 0
* If string1 is Null - InStr returns Null
* If string2 is "" - InStr returns start
* If string2 is Null - InStr returns Null
* If string2 is not found - InStr returns 0
* If string2 is found within string1 - InStr returns the position at which match is found
* If start > Len(string1) - InStr returns 0

Tip: Also look at the InStrRev function
 
hi

还是没解决啊,你这个好像不是vb吧?语言都看不懂的
 
there's a function called replace in VB that can instantly replace every "xxx" by "XXXX"

why bother using a loop..
 
谢谢

嗯,因为老师就要求用for..next ,或者do loop,还注明不让用function,以为这是考循环那一块的,我做完后,结果都对,可是就是之后又显示出一条错误信息,到底为什马?
 
Mid returns a value thus you can't use mid = xxx

try word(x) where word = ur string variable, x is the position

i'm pretty sure you can treat strings as an array of char
 
Private Sub cmdNewWord_Click()

strWord = txtText.Text
strLetter = txtFindLetter.Text
strReplaceLetter = txtReplaceWith.Text
intLength = Len(strWord)

For intCount = intPosition To intLength

intPosition = InStr(1, strWord, strLetter)

if intpostion = 0 then exit for 'added this line

'get the number of the position of the Finding letter
Mid(strWord, intPosition, 1) = strReplaceLetter
'The replace letter will replace the finding word
lblNewWord.Caption = strWord

Next intCount
End Sub
 
后退
顶部