I want my program to search for the next " " (space) after 100 characters and then add an "a" to it. I need it to do that every 100 characters
how do I do that?
Printable View
I want my program to search for the next " " (space) after 100 characters and then add an "a" to it. I need it to do that every 100 characters
how do I do that?
Did you meant that or didn't I understand ya?Code:Private Sub Text1_Change()
i = i + 1
If i = 100 Then
i = 0
Text1.SelText = " a"
End if
End Sub
I haven't checked it really, but that should be the guide line ..
Dim Mystr As String
Dim Counter As Integer
Dim CounterFromLastSpace As Integer
CounterFromLastSpace = 0
Mystr = "bla bla lbadfdsf df dsfsdf sdfdsfsdfdsf dsff gfhfghghgfgfg ghgfh gfh fghgf fghfgh fgh gfh fgh fgh fgfgh fghfgh fghfgh fgh fgh fgh fgh fgh fgh fghg hfgh fgh fgh fgh fgh fgh fgh ...."
For Counter = 0 To Len(Mystr)
CounterFromLastSpace = CounterFromLastSpace + 1
If Mid(Mystr, Counter + 1, 1) = " " Then
If CounterFromLastSpace = 100 Then
Mystr = Left(Mystr, Counter) & " a " & Right(Mystr, Len(Mystr) - Counter)
CounterFromLastSpace = 0
End If
End If
Next
I tried that , what I need is so that every 100 characters it adds a " a" to the thing and it will do the same for the 200th 300th 400th 500th 600th
I hope this time was more clear
sorry, the if statment should be:
If CounterFromLastSpace >= 100 Then
I'm sorry, that still doesn't work
o.k, so maybe I did't understand you ...
lets see.
if you have:
"here is my code for the program that you want"
and lets say that you want to add "a" every 10 char, do you mean:
"here is my a code for a the progra a m that you a want"
OR
"here is my a code for a the program a that you a want"
??
And I think I know what is the problem with my fornext loop, I keep changing the length of the variable, you should change it a bit something like:
Dim Mystr As String
Dim Counter As Integer
Dim CounterFromLastSpace As Integer
dim until as integer
CounterFromLastSpace = 0
Mystr = "bla bla lbadfdsf df dsfsdf sdfdsfsdfdsf dsff gfhfghghgfgfg ghgfh gfh fghgf fghfgh fgh gfh fgh fgh fgfgh fghfgh fghfgh fgh fgh fgh fgh fgh fgh fghg hfgh fgh fgh fgh fgh fgh fgh ...."
until=Len(Mystr)
For Counter = 0 To until
CounterFromLastSpace = CounterFromLastSpace + 1
If Mid(Mystr, Counter + 1, 1) = " " Then
If CounterFromLastSpace >= 100 Then
Mystr = Left(Mystr, Counter) & " a " & Right(Mystr, Len(Mystr) - Counter)
CounterFromLastSpace = 0
End If
End If
Next
End Sub
this is what I would like:
"here is my a code for a the program a that you a want"
now is that the code for it in your last post?
yes,
Again, I havenet fully tested it but it should work , or at least the logic is there .. :-)
I think it's faster because it doesn't do it char by char but Step 99 it does 99 at a time. (not sure if it's faster by the way :( and I'm not taking down your code or saying mine is 'better')Code:'This'll work!
For x = 0 To Len(Text1.Text) Step 100
Text1.SelStart = x
Text1.SelLength = 1
If Text1.SelText = " " Then
Text1.SelStart = x
Text1.SelText = " a"
End If
Next x
[Edited by Jop on 09-09-2000 at 12:31 PM]
first of all THANK YOU !
1. I never used this property of a text box (never new it exists) !.
2. Your code looks nice, but I don't think it handles if there is no space exactly in the 100th character ... then it just skips it (correct me if I am wrong I just read it).
maybe a combination of both our code will help him, he can
check if it is not a " " than move one forword etc ... until it is " " and then add the "a" ..
sounds good ?
My code had some flaws :)
I believe this has no flaws! (but I may be wrong hehe)Code:For x = 100 To Len(Text1.Text) Step 98
Text1.SelStart = x
Text1.SelLength = 1
If Text1.SelText = " " Then
Text1.SelStart = x
Text1.SelText = " a"
End If
Next x
what about the case in which there is no " ", will it put it at the next " " ?
that's the way I understood he want it ..
:-)
Code:
Option Explicit
Private Sub Command1_Click()
Dim intCre, intCharCount, intStringLen, _
sTextString, sCharHolder, sNewTextString
'load up some junk for testing making sure no letter a is involved
'as it's easier to test if the field is bare
For intCre = 1 To 400
Text1.Text = Text1.Text & intCre
Next intCre
'load string and get length
sTextString = Text1.Text
intStringLen = Len(sTextString)
'for the length of the string count every 100 spaces
'and create the new string as you go
For intCre = 1 To Len(sTextString)
sCharHolder = Left(sTextString, 1)
intCharCount = intCharCount + 1
sNewTextString = sNewTextString & sCharHolder
'if the count is 100 and an "a" as asked and reset the counter
If intCharCount = 100 Then
intCharCount = 0
sNewTextString = sNewTextString & "a"
End If
'continue on through the string
sTextString = Right(sTextString, intStringLen - intCre)
Next intCre
'when finished with the loop add the new string to text2.text or
'whatever you want to do with it
Text2.Text = sNewTextString
''this is just for checking to see it the above works (and it does)
'validation of process
sTextString = Text2.Text
intStringLen = Len(sTextString)
intCharCount = 1
For intCre = 1 To Len(sTextString)
sCharHolder = Left(sTextString, 1)
'if we find an a let's count them
If Asc(sCharHolder) = 97 Then
intCharCount = intCharCount + 1
End If
'continure on
sTextString = Right(sTextString, intStringLen - intCre)
Next intCre
'how many letterw were found.
'should be the same as the string length /100
MsgBox intCharCount & " letters a involved"
MsgBox Fix(intStringLen / 100)
End Sub
Oh, **** my code, it sucks, don't use it.
This'll do better I think :)
Code:Dim Counter As Integer
Dim foundat As Long, foundatL As Long
Dim x As Long
Text1 = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
For Counter = 1 To Len(Text1)
foundat = InStr(foundat + 1, Text1, " ")
If foundat > foundatL + 98 Then
Text1 = Left(Text1, foundat) & "a " & Right(Text1, Len(Text1) - foundat)
foundatL = foundat
End If
Next
Thanks to everone for your help, I really appriciate it. and one more time THANKS!!!