Legion4400 Posted June 16, 2012 Posted June 16, 2012 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)
water Posted June 16, 2012 Posted June 16, 2012 (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 June 16, 2012 by water My UDFs and Tutorials: Reveal hidden contents UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Legion4400 Posted June 16, 2012 Author Posted June 16, 2012 (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 June 16, 2012 by Legion4400
water Posted June 16, 2012 Posted June 16, 2012 (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 June 16, 2012 by water My UDFs and Tutorials: Reveal hidden contents UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now