am632 Posted May 1, 2016 Posted May 1, 2016 Hi, I'm wanting to replace characters in a string which occure on more than 1 occasion, so for example if I have the following string. XXXYYYYXXZZXXXXXUUU So is there a function I can use to replace say 2 x's with the letter 'W' and 3 x's with the letter 'R' for example? but if there are 5 X's like in the example could this be replaced with the letters 'WR' I had a look at all the string functions in the help file but I cant find anything that would be suitable. I essentially need to search the string starting from the beginning for the occurances because the string can change depending on some variables from the user. Thanks
water Posted May 1, 2016 Posted May 1, 2016 (edited) I think this should be possible with StringReplace. Start with the longest: $sString = StringReplace($sString, "xxxxx", "WR") $sString = StringReplace($sString, "xxxx", "??") $sString = StringReplace($sString, "xxx", "R") $sString = StringReplace($sString, "xx", "W") Edited May 1, 2016 by water My UDFs and Tutorials: Spoiler 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
mikell Posted May 1, 2016 Posted May 1, 2016 (edited) A precise use of StringReplace should do it (from left to right, always) $str = "XXXYYYYXXZZXXXXXUUU" $str = StringReplace($str, 'XXX', 'W') $str = StringReplace($str, 'XX', 'R') Msgbox(0,"", $str) Edit water I didn't see your post Edited May 1, 2016 by mikell
am632 Posted May 3, 2016 Author Posted May 3, 2016 Hi, Thanks for that it works fine. I did originally try StringReplace but it didn't cross my mind to start with the longest string frist. Thanks
water Posted May 3, 2016 Posted May 3, 2016 My UDFs and Tutorials: Spoiler 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