Jump to content
Sign in to follow this  
Legion4400

replace multiple substrings in a string

Recommended Posts

Legion4400

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)

Share this post


Link to post
Share on other sites
water

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:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Legion4400

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

Share this post


Link to post
Share on other sites
water

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:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.