Solved

PowerShell Convert String Into Array

Posted on 2013-05-21
3
643 Views
Last Modified: 2013-05-21
I have given up! I have a string with a semicolon ; as the delimiter - how do I use that to turn it into an array?

$var = One; Two; Three

What I have:
echo $var =
One; Two; Three

What I want:
echo $var =
One;
Two;
Three
0
Comment
Question by:brendan-amex
3 Comments
 
LVL 11

Accepted Solution

by:
apathy42 earned 500 total points
ID: 39186022
$var.split(";") will turn it into the array.
0
 

Author Closing Comment

by:brendan-amex
ID: 39186027
Good quick answer. Thank you!
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39186029
You really want the semicolon as part of the output? And what about the trailing (or leading) space? .Split or -split will both remove the delimiter. The result is
One
.Two
.Three

Open in new window

where the dot represents a space. We can use -split to have a two-char delimiter:
PS> "One; Two; Three" -split "; "
One
Two
Three

Open in new window

The .Split method is not able to do that, it accepts only arrays of single delimiting chars, which are then used separately.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question