[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-03-16
9
Medium Priority
?
871 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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 18

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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

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.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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.
Suggested Courses

649 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