If I have a variable string, ie:
$test="one,two,thr"
or
$test="one,two,thr,four,five"
What is the best way to find the number of "," in the string? index?
Thanks in advance,
Martin
"Frank" <Fr...@discussions.microsoft.com> wrote in message
news:6E711425-483C-4B06...@microsoft.com...
---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
F> Hi,
F>
F> If I have a variable string, ie:
F>
F> $test="one,two,thr"
F>
F> or
F>
F> $test="one,two,thr,four,five"
F>
F> What is the best way to find the number of "," in the string? index?
F>
F> Thanks in advance,
F>
The count starts at 0.
The first object is 0
The second is 1
On and on :)
What are you actually counting?
In
"one,two,three".Split(",") | measure-object
You are splitting on the "," - so, there are three itens, one, two, and three.
If your requirements are really this simple (the number of commas in a string)
you would have to take the output and subtract 1 like
("one,two,three".Split(",") |measure-object).Count -1
Karl
"one,two,three".Split(",").Count - 1
--
Thanks,
Roman Kuzmin
http://code.google.com/p/farnet/
PowerShell and .NET in FAR Manager
An excellent point - I should not created my own example for the OP instead
of copying :)
Thanks
Karl
> Performance remark. Using of Measure-Object is redundant - Split()
> returns an array, property Count perfectly works for it:
>
> "one,two,three".Split(",").Count - 1
>