leuce Posted October 20, 2012 Share Posted October 20, 2012 G'day everyone When I use StringSplit to create an array, the string is split at every delimiter. But is there a simple way to tell the StringSplit to split only at the first delimiter? In other words, suppose I have this string: $foo = 'abc#def#ghi#jkl#mno#pqr#stu#vwx#yz' and I want this array: $bar1 = 'abc' $bar2 = 'def#ghi#jkl#mno#pqr#stu#vwx#yz' ...how would I do that? I suppose I could just split by '#' and then use _ArrayToString on all array items except the first one, but is there a simpler way to do it? Thanks Samuel Link to comment Share on other sites More sharing options...
water Posted October 20, 2012 Share Posted October 20, 2012 Use function StringInStr and search for the first occurrence of the delimiter then split manually. 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 Link to comment Share on other sites More sharing options...
leuce Posted October 20, 2012 Author Share Posted October 20, 2012 Use function StringInStr and search for the first occurrence of the delimiter then split manually.This is going to sound very weird, but... I don't know how to split manually. Can you please show me? Link to comment Share on other sites More sharing options...
water Posted October 20, 2012 Share Posted October 20, 2012 Use functions StringLeft and StringMid: $foo = 'abc#def#ghi#jkl#mno#pqr#stu#vwx#yz' $iPos = StringInStr($foo, "#") $bar1 = Stringleft($foo, $iPos-1) $bar2 = StringMid($foo, $iPos+1) ConsoleWrite($bar1 & @LF) ConsoleWrite($bar2 & @LF) 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 Link to comment Share on other sites More sharing options...
kylomas Posted October 20, 2012 Share Posted October 20, 2012 leuce, Another way #include <array.au3> local $bar2 = 'def#ghi#jkl#mno#pqr#stu#vwx#yz' $a10 = stringsplit(stringreplace($bar2,'#',0x0,1),0x0) _arraydisplay($a10) This assumes that you do NOT have a hex 0 in the split string. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
leuce Posted October 20, 2012 Author Share Posted October 20, 2012 Use functions StringLeft and StringMid...Thanks! Link to comment Share on other sites More sharing options...
leuce Posted October 20, 2012 Author Share Posted October 20, 2012 Another way #include <array.au3> local $bar2 = 'def#ghi#jkl#mno#pqr#stu#vwx#yz' $a10 = stringsplit(stringreplace($bar2,'#',0x0,1),0x0) _arraydisplay($a10) Very clever :-) Link to comment Share on other sites More sharing options...
Malkey Posted October 21, 2012 Share Posted October 21, 2012 Here is a third and fourth method of splitting a string on the first "#". #include <array.au3> Local $foo = 'abc#def#ghi#jkl#mno#pqr#stu#vwx#yz' Local $bar1_2 = StringRegExp($foo, '(.+?)#(.+)', 3) _ArrayDisplay($bar1_2) ;or Local $bar1 = StringRegExpReplace($foo, '(#.+)$', "") ; Erase from first "#", to end Local $bar2 = StringRegExpReplace($foo, '(^.+?#)', "") ; Erase from beginning, to first "#" MsgBox(0, "Results", "$foo = " & $foo & @LF & _ "$bar1 = " & $bar1 & @LF & _ "$bar2 = " & $bar2) ergo 1 Link to comment Share on other sites More sharing options...
ergo Posted February 10, 2022 Share Posted February 10, 2022 On 10/21/2012 at 8:20 AM, Malkey said: Local $bar1_2 = StringRegExp($foo, '(.+?)#(.+)', 3) Minor changes if you want to get a result on strings without delimiters. StringRegExp($foo,'(.+?)(?:#(.+))?$', $STR_REGEXPARRAYMATCH) Link to comment Share on other sites More sharing options...
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