Jump to content

Recommended Posts

Posted

im looking to replace multiple (over 100) substrings within a string.is there sa better way that his?

im just a beginner. ty

Local $file = FileOpen("test.txt", 0)
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
Local $chars = FileRead($file, 1000)
Local $text = StringReplace($chars, "x"& @CR & @LF &"name    abc", "x"& @CR & @LF &"name     def")
Local $text2 = StringReplace($chars, "y"& @CR & @LF &"name   abc", "y"& @CR & @LF &"name     def")
Local $text2 = StringReplace($chars, "z"& @CR & @LF &"name   abc", "z"& @CR & @LF &"name     def")
.....
FileClose($file)
Posted (edited)

Welcome to AutoIt and the forum.

Do it in a loop. Fill array $aReplace with the 100 characters and at the end you have the replaced strings in array $aReplaced.

Local $aReplace[100] = ["x", "y", "z"]
Local $aReplaced[100]
Local $file = FileOpen("test.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
Local $chars = FileRead($file, 1000)
For $iIndex = 0 to UBound($aReplace)-1
    $aReplaced[$iIndex] = StringReplace($chars, $aReplace[$iIndex] & @CR & @LF & "name  abc", $aReplace[$iIndex] & @CR & @LF & "name     def")
Next
FileClose($file)
Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

ty

name abc was just an example, most of the substrings are different, about 100 of them, some of them are duplicates. in the loop i see that "name abc" will always be present, same for name def.

im sry it was my mystake from the begining. what im trying to do is delete 1 character from the end of each substring, something like :

"x"&@CR&@LF&"name abc" turn to "x"&@CR&@LF&"name ab"

"x"&@CR&@LF&"name efrga" turn to "x"&@CR&@LF&"name efrg"

"x"&@CR&@LF&"name 23rfs" turn to "x"&@CR&@LF&"name 23rf"

sorry i wasnt exact from the start

Edited by Legion4400
Posted (edited)

No problem. Then use a two-dimensional array:

Local $aReplace[100][2] = [["x" & @CRLF & "name abc", "x" & @CRLF & "name ab"], _
    ["x" & @CRLF & "name efrga", "x" & CRLF & "name efrg"], _
    ["x" & @CRLF & "name 23rfs", "x" & @CRLF & "name 23rf"]]
Local $aReplaced[100]
Local $file = FileOpen("test.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
Local $chars = FileRead($file, 1000)
For $iIndex = 0 to UBound($aReplace)-1
    If $aReplace[$iIndex][0] <> "" Then $aReplaced[$iIndex] = StringReplace($chars, $aReplace[$iIndex][0], $aReplace[$iIndex][1])
Next
FileClose($file)
Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...