On 03/16/2015 03:29 PM, Norbert Klasen wrote:
> Hi,
> % i = 'c: " a.gif"'.safesplit(' ','"').length;
> 2
> % i = 'c:\\ " a.gif"'.safesplit(' ','"').length;
> 1
> Shouldn't both return an array of length 2?

One form of the safesplit method is available here:


While the current method is actually implemented in Java, I presume the
logic is the same.

A '\' is an escape, and since you have it in there twice the first escapes
the second to at least get the backslash into the string. safesplit,
then, sees that as an escape of the next character, a space, which is your
delimiter between tokens.

The following two options seem to work, where "work" means split into two
tokens, though I'm a little curious how this ugly path came to be in the
first place:

'"c:\\ " " a.gif"'.safesplit(' ','"')
'c:\\\\ " a.gif"'.safesplit(' ','"')[0]
Of course the right thing to do is to first fix the incorrect use of
escapes (backslashes):

'c:\\ " a.gif"'.replace('\\','/').safesplit(' ', '"')
Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below...