Jump to content
Sign in to follow this  
leuce

How to StringSplit only once

Recommended Posts

leuce

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

Share this post


Link to post
Share on other sites
water

Use function StringInStr and search for the first occurrence of the delimiter then split manually.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
leuce

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?

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
leuce

Use functions StringLeft and StringMid...

Thanks!

Share this post


Link to post
Share on other sites
Malkey

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)

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  

×