|
-
Aug 18th, 2009, 05:03 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Find (partial string) and replace (whole string)
Hey All,
I have a text file that looks like this...
0000001001 eeee
0000001002 aaaa
0000001003 bbbb
0000001004 cccc
0000001005 dddd
How can I search for just the numbers on the left, but replace the whole
string when I find it? In other words, the letters on the right will change,
and I will not know what they are.
(example) I need to change the line "0000001003 bbbb" to "0000001003 zzzz"
but the only info I have is the numbers "0000001003".
Thanks in advance,
Ron
I'm using VB5.
Here is some code I found on here that almost works, except when I use it
it just puts the z's in front of the b's (0000001003 zzzz bbbb)...
Code:
Public Function ReplaceStringInFile(ByVal StrFileName As String, _
ByVal StrFind As String, ByVal strReplace As String) As String
Dim strTest As String
Open StrFileName For Binary As #1
strTest = Space(LOF(1))
Get #1, , strTest
strTest = Replace(strTest, StrFind, strReplace)
Put #1, 1, strTest
Close #1
ReplaceStringInFile = strTest
End Function
Private Sub Command1_Click()
ReplaceStringInFile "C:\test.txt", "0000001003", "0000001003 zzzz"
End Sub
And since I'm using VB5, I'm using this Replace function...
Code:
Public Function Replace(ByVal sIn As String, ByVal sFind As _
String, ByVal sReplace As String, Optional nStart As _
Long = 1, Optional nCount As Long = -1, _
Optional bCompare As VbCompareMethod = vbBinaryCompare) As _
String
Dim nC As Long, nPos As Long
Dim nFindLen As Long, nReplaceLen As Long
nFindLen = Len(sFind)
nReplaceLen = Len(sReplace)
If (sFind <> "") And (sFind <> sReplace) Then
nPos = InStr(nStart, sIn, sFind, bCompare)
Do While nPos
nC = nC + 1
sIn = Left(sIn, nPos - 1) & sReplace & _
Mid(sIn, nPos + nFindLen)
If nCount <> -1 And nC >= nCount Then Exit Do
nPos = InStr(nPos + nReplaceLen, sIn, sFind, _
bCompare)
Loop
End If
Replace = sIn
End Function
Last edited by rdcody; Aug 18th, 2009 at 05:06 PM.
-
Aug 19th, 2009, 05:34 AM
#2
Re: Find (partial string) and replace (whole string)
you can try, replace
strTest = Replace(strTest, StrFind, strReplace)
with
if instr(strtest, strfind) > 0 then strtest = strreplace
you will then not be using the replace function at all
Last edited by westconn1; Aug 19th, 2009 at 06:36 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 19th, 2009, 09:11 AM
#3
Thread Starter
Addicted Member
Re: Find (partial string) and replace (whole string)
Hey westconn1, thanks for responding...
It replaced the first line not 0000001003 bbbb ...
0000001003 zzzz
0000001002 aaaa
0000001003 bbbb
0000001004 cccc
0000001005 dddd
-
Aug 19th, 2009, 09:43 AM
#4
Thread Starter
Addicted Member
Re: Find (partial string) and replace (whole string)
I got it to work by adding another function to find the full string,
and then the replace function worked properly...
Code:
Private Function FindText(ByVal start_at As Integer)
Dim pos As Integer
Dim target As String
Open "C:\test.txt" For Binary As #1
txtBody.Text = Input$(LOF(1), #1)
Close #1
target = txtTarget.Text '= 0000001003
pos = InStr(start_at, txtBody.Text, target)
If pos > 0 Then
' We found it.
TargetPosition = pos
txtBody.SelStart = TargetPosition - 1
txtBody.SelLength = 15 'Len(target)
txtBody.SetFocus
txtFullString.Text = txtBody.SelText
Else
' We did not find it.
MsgBox "Not found."
txtBody.SetFocus
End If
End Function
and calling it like this...
Code:
Private Sub Command1_Click()
FindText 1
DoEvents
ReplaceStringInFile "C:\test.txt", txtFullString.Text, txtReplace.Text
End Sub
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
|