Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[RC2] Split and single quote

7 views
Skip to first unread message

Jean

unread,
Sep 27, 2006, 4:16:12 PM9/27/06
to
Bug or limitation ?

Typing:

$a='1`n2`n3'
$a.Split(`n)

result is:

1

2

3

result expected :

1
2
3

Typing:

$a='1$var2$var3'
$a.Split('$var')

result is:

1

2

3

result expected :

1
2
3

Regards,

--
Jean - JMST
Belgium


Jean

unread,
Sep 27, 2006, 4:22:14 PM9/27/06
to
> $a.Split(`n)

read :

$a.Split('`n')

Andrew Watt [MVP]

unread,
Sep 27, 2006, 4:38:14 PM9/27/06
to
Another interesting observation is the difference in behaviour when
using Split() using paired double quotes or paired apostrophes.

PS Env:\> $a = 'a`nb`nc'
PS Env:\> $a.Split("`n")
a`nb`nc
PS Env:\> $a.Split('`n')
a

b

c
PS Env:\>

Andrew Watt MVP

Wei Wu [MSFT]

unread,
Sep 27, 2006, 4:46:01 PM9/27/06
to
This is by design.
`n is no longer processed in single-quoted strings.

PS C:\> $a="1`n2`n3"
PS C:\> $a.split("`n")
1
2
3

--
Wei Wu [MSFT]
Windows PowerShell Team
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Jean

unread,
Sep 27, 2006, 5:01:30 PM9/27/06
to
> This is by design.
> `n is no longer processed in single-quoted strings.

That's right, James Truher told me that recently.

But if I write (with single quotes) :

$a='1`n2`n3'

output is

1`n2`n3

and so if I write (with single quotes)

$a.Split('`n')

I expect Split searches `n and not <cr> as '`n' is `n

And it's as it works but some extra spaces are added in output ... why
?


Regards,

Jean

unread,
Sep 27, 2006, 5:04:29 PM9/27/06
to
> Another interesting observation is the difference in behaviour when
> using Split() using paired double quotes or paired apostrophes.

See Wei Wu answer :

"This is by design.
`n is no longer processed in single-quoted strings."

and from RELEASENOTES.RTF :

"-- Windows PowerShell does not interpret a backtick (`) as an escape
character when it appears within single quotation marks. This applies
to all single-quoted strings, including strings in scripts."

Jean

unread,
Sep 27, 2006, 5:06:42 PM9/27/06
to
> some extra spaces

read : some extra lines

Andrew Watt [MVP]

unread,
Sep 27, 2006, 5:07:49 PM9/27/06
to
Thanks, Wei Wu. But have a look at the following:

PS C:\Documents and Settings\Andrew Watt> $DoubleQuotes = "a`nb`nc"
PS C:\Documents and Settings\Andrew Watt> $DoubleQuotes.Split("`n")
a
b
c
PS C:\Documents and Settings\Andrew Watt> $DoubleQuotes.Split('`n')
a
b
c
PS C:\Documents and Settings\Andrew Watt> $SingleQuotes = 'a`nb`nc'
PS C:\Documents and Settings\Andrew Watt> $SingleQuotes.Split("`n")
a`nb`nc
PS C:\Documents and Settings\Andrew Watt> $SingleQuotes.Split('`n')
a

b

c
PS C:\Documents and Settings\Andrew Watt>

If $DoubleQuotes.Split('`n') doesn't process a single-quoted string
then the supposed separator should, I think, be literal back tick
followed by literal n. Since that literal (unprocessed) sequence
doesn't occur in $SingleQuotes then the whole string should be output
unchanged, I think. But $DoubleQuotes.Split('`n') seems to process
the `n.

For $SingleQuotes.Split('`n') why is there a blank line between the a
the b and the c? If I have

$b = 'a,b,c'

and use

$b.Split(',')

the separator isn't displayed.

PS C:\Documents and Settings\Andrew Watt> $b = 'a,b,c'
PS C:\Documents and Settings\Andrew Watt> $b.Split(',')
a
b
c
PS C:\Documents and Settings\Andrew Watt>

Why should it be for $SingleQuotes.Split('`n')?

Thanks

Andrew Watt MVP

dreeschkind

unread,
Sep 27, 2006, 5:52:02 PM9/27/06
to
I'd call this a bug.

--
greetings
dreeschkind

/\/\o\/\/ [MVP]

unread,
Sep 27, 2006, 6:11:02 PM9/27/06
to
Me too
(more an addition to the old one, making it worse)

gr /\/\o\/\/

Jean

unread,
Sep 27, 2006, 6:20:45 PM9/27/06
to
> Bug or limitation ?

FYI I "fix" that by specifying StringSplitOptions in Split :

$a='1`n2`n3'
$a.Split('`n',[StringSplitOptions]::RemoveEmptyEntries)

1
2
3

$a='1$var2$var3'
$a.Split('$var',[StringSplitOptions]::RemoveEmptyEntries)

James Truher

unread,
Sep 27, 2006, 6:19:05 PM9/27/06
to
nope - this behavior is correct - here's why

when string.split is used like this:
"this is a test".split('`n')
the '`n' is a char[]
thus

'1`n2`n3'.split('`n')
splits first on the "`"
then then on the "n"
you can see this if you do the following:

PS> '1`n2`n3'.split('`')
1
n2
n3
PS> '1`n2`n3'.split('n')
1`
2`
3
then finally
PS> '1`n2`n3'.split('`n')
1

2

3

changing this to double quotes changes everything:

PS> "1`n2`n3".split("`n")
1
2
3

and since in the next string, there aren't any newlines ("`n"), no splitting
is done!
PS> '1`n2`n3'.split("`n")
1`n2`n3

--
--
James Truher [MSFT]
Windows PowerShell Development


Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"dreeschkind" <drees...@discussions.microsoft.com> wrote in message
news:A3D3AB56-6251-4FBB...@microsoft.com...

Jean

unread,
Sep 27, 2006, 6:33:08 PM9/27/06
to
> nope - this behavior is correct - here's why

... as an optical illusion :-)

I understand now ...

Great thanks.

dreeschkind

unread,
Sep 27, 2006, 6:47:01 PM9/27/06
to
"James Truher" wrote:

> nope - this behavior is correct - here's why

Oops, did I say anything? :P

Nice gotcha!

--
greetings
dreeschkind

Jean

unread,
Sep 28, 2006, 9:45:04 AM9/28/06
to
> FYI I "fix" that by specifying StringSplitOptions in Split :

''|gm Split|fl

Definitions having "String[] separator" are what I was searching for :

$a='1`n2`n3'
$a.Split([string[]]'`n',[StringSplitOptions]::None)

returns :

1
2
3


Thanks all again for replies.

0 new messages