Solved

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

Posted on 2011-03-16
9
860 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
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
 
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 70

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now