Solved

Turn a non-delimited string into an array in powershell?

Posted on 2011-03-16
9
866 Views
Last Modified: 2012-08-14
Alright, so I mention this in most of my questions about powershell, but I am new to it.

If I have a string, how do  I convert it to an array.  Example below:
String:
aNgie#35

Needs to become:
"a","N","g","i","e","#","3","5"

I was looking at the split function, but how do I use that if we aren't using a delimiter?
0
Comment
Question by:childersj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35151051
I believe that if you pass an empty string to split as the delimiter it will split each character individually as you want.  Then join back with quotes between and before and after.


'"' + mystring.Split("").Join('","') + '"'

Open in new window

0
 

Author Comment

by:childersj
ID: 35151301
That didn't work.  I thought of something like that myself.  I tried to do something like the following

$strRandom = some combination of letters numbers and symbols
$arrRandomn = $strRandom.split("").join(",")

also tried this

$arrRandomn = $strRandom.split().join(",")

It says system.object[]  doesn't contain a method named 'Split'.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35151641
Try adding a tostring call:

$arrRandomn = '"' + $strRandom.tostring().split("").join('","') + '"' 

Open in new window

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 16

Accepted Solution

by:
sjklein42 earned 168 total points
ID: 35151772
I can't test it myself right now (sorry) but there seems to be a ToCharArray function that many people use for exploding a string into an array of characters.  I can't find source code for ToCharArray so maybe it is built in.  Please see if either of these work for you:

$arrRandomn = '"' + $strRandom.ToCharArray().join('","') + '"' 

or

$arrRandomn = '"' + $strRandom.tostring().ToCharArray().join('","') + '"' 

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 35151881
$s -split "(?<=.)(?=.)"
0
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 166 total points
ID: 35152193
So, the

"aNgie#35" -split ""

based solutions are not quite correct, as they produce an array of 10 elements (first and last are empty strings).

My

"aNgie#35"  -split "(?<=.)(?=.)"  

produces the correct array of 8 elements.

If we change the first priod (.) to any other regex expression, such as \d for digits, we can split the following string at numbers, but still keep the numbers in the result:

"gg7kjh8j9k4j5j8" -split "(?<=\d)(?=.)"
gg7
kjh8
j9
k4
j5
j8
0
 
LVL 17

Assisted Solution

by:Learnctx
Learnctx earned 166 total points
ID: 35152290
Try this.
$string = "aNgie#35"
$arrLetters += $string.GetEnumerator()

Open in new window

This should put each letter as a new item of the $arrLetters array. Or if you just want to know how to split up a string, you can use the GetEnumerator() function ($string.GetEnumerator()) without putting it into an array.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35687982
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

687 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