Solved

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

Posted on 2011-03-16
9
859 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

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

Assisted Solution

by:soostibi
soostibi earned 166 total points
Comment Utility
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 16

Assisted Solution

by:Learnctx
Learnctx earned 166 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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 …
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now