Jump to content

How to StringSplit only once


leuce
 Share

Recommended Posts

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

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

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 2022-02-19 - Version 1.6.1.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 (NEW 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

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

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)
Link to comment
Share on other sites

  • 9 years later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...