Jump to content

Recommended Posts

Posted

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

Posted

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

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

  On 10/20/2012 at 1:43 PM, 'water said:

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?

Posted

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:

  Reveal hidden contents

 

Posted

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)
  • 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
  • Recently Browsing   0 members

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