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
read :
$a.Split('`n')
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
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.
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,
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."
read : some extra lines
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
--
greetings
dreeschkind
gr /\/\o\/\/
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)
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...
... as an optical illusion :-)
I understand now ...
Great thanks.
> nope - this behavior is correct - here's why
Oops, did I say anything? :P
Nice gotcha!
--
greetings
dreeschkind
''|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.