?
Solved

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

Posted on 2011-03-16
9
Medium Priority
?
868 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
Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

 
LVL 16

Accepted Solution

by:
sjklein42 earned 672 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 664 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 664 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

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 …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

777 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