Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

[RC2] Split and single quote

7 vues
Accéder directement au premier message non lu

Jean

non lue,
27 sept. 2006, 16:16:1227/09/2006
à
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

non lue,
27 sept. 2006, 16:22:1427/09/2006
à
> $a.Split(`n)

read :

$a.Split('`n')

Andrew Watt [MVP]

non lue,
27 sept. 2006, 16:38:1427/09/2006
à
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]

non lue,
27 sept. 2006, 16:46:0127/09/2006
à
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

non lue,
27 sept. 2006, 17:01:3027/09/2006
à
> 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

non lue,
27 sept. 2006, 17:04:2927/09/2006
à
> 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

non lue,
27 sept. 2006, 17:06:4227/09/2006
à
> some extra spaces

read : some extra lines

Andrew Watt [MVP]

non lue,
27 sept. 2006, 17:07:4927/09/2006
à
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

non lue,
27 sept. 2006, 17:52:0227/09/2006
à
I'd call this a bug.

--
greetings
dreeschkind

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

non lue,
27 sept. 2006, 18:11:0227/09/2006
à
Me too
(more an addition to the old one, making it worse)

gr /\/\o\/\/

Jean

non lue,
27 sept. 2006, 18:20:4527/09/2006
à
> 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

non lue,
27 sept. 2006, 18:19:0527/09/2006
à
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

non lue,
27 sept. 2006, 18:33:0827/09/2006
à
> nope - this behavior is correct - here's why

... as an optical illusion :-)

I understand now ...

Great thanks.

dreeschkind

non lue,
27 sept. 2006, 18:47:0127/09/2006
à
"James Truher" wrote:

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

Oops, did I say anything? :P

Nice gotcha!

--
greetings
dreeschkind

Jean

non lue,
28 sept. 2006, 09:45:0428/09/2006
à
> 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 nouveau message