30

Convert a string to a character array

Hello everyone! We are back with a series of Brain Teasers. In the next 3 weeks we will publish one teaser per week.
It will run until Friday and the winner will be announced on the next Monday, taking home the eBook version of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll. We would like to thank our sponsor Packt, one of the most prolific and fast-growing tech book publishers in the world, for providing such a cool prize.

OK, time to start your engine, here goes the first one :)

You have a string, “PowerShell”, you need to break it to its individual characters so the result is a collection of System.Char objects:

P
o
w
e
r
S
h
e
l
l

Requirements

1. You cannot cast the string to char array, e.g [char[]]”PowerShell”
2. You cannot use the String.ToCharArray method

The most shortest/elegant solution wins.

Good luck!

Filed in: Brainteasers Tags: ,

One Pingback/Trackback

  • http://twitter.com/JohnLudlowUK John Ludlow

    $s = “Powershell”; for ($i = 0; $i -lt $s.length; $i++) { $s[$i] }

  • http://twitter.com/p0w3rsh3ll _Emin_

    “PowerShell” -split”(?<=G.{1})"

  • http://HuddledMasses.org Joel “Jaykul” Bennett

    “PowerShell”[0..9]

    • dvsdee

      Sorry couldn’t resist: “PowerShell”[-1..-10]

  • http://twitter.com/mattifestation Matt

    ‘PowerShell’-split’(w{1})’|?{$_}

  • Crow

    ‘PowerShell’-split’B’

    • BacktoPoSh

      Correct me if I’m wrong, but this outputs String objects, not Char objects

      • Crow T

        That is right, the split op returns strings. To get an array of Chars
        from a String of any length you can do this:

        ($s=’PowerShell’)[0..$s.Length]

  • Crow T

    ‘PowerShell’-split’B’

  • kevin

    For any string $test:

    $test[0..($test.length)]

  • kevin

    Actually, you don’t need the parenthesis:

    $test[0..$test.length]

  • http://twitter.com/bielawb Bartek Bielawski

    ‘PowerShell’.GetEnumerator() ;)

    • Johnny Leuthard

      I think this one is best as it will take any string. Thetwo above it are great however if the string length changes it will break.

    • Crow T

      In your case GetEnumerator returns a CharEnumerator, not a Char[]
      ‘PowerShell’.GetEnumerator()[1]

  • Mike F Robbins

    ([regex]::Matches(“PowerShell”,’.',’RightToLeft’) | % {$_}) -join ” + “`n”

  • Tobias Weltner

    “PowerShell” -split ‘(?<=.)(?=.)'

  • Tobias Weltner

    “PowerShell” -split ‘(?<=.)(?=.)'

    sorry if this is a repost. Somehow, my post vanished… :-)

  • Larry

    $test[0..($test.length-1)]

  • Crow T

    ($s=’PowerShell’)[0..$s.Length]

  • sahal

    “Powershell” -split “”

    • sahal

      Best one i see after reading others answers is:

      “Powershell”[0.."Powershell".Length]

      or
      $str[0..$str.length]

    • Tobias Weltner

      Like this! Unfortunately, it returns two more (blank) characters…

  • http://www.facebook.com/vern.anderson.395 Vern Anderson

    “PowerShell”.GetEnumerator()

  • Peter

    “Powershell” -split “B” | foreach {[char[]]$arr += $_}

  • http://www.facebook.com/nat.davis.10 Nat Davis

    I love regex, how about this

    [regex]::split(“PowerShell”,”")

  • Pingback: PowerShellでStringをChar[]に変換する « guitarrapc.wordpress.com

  • http://twitter.com/guitarrapc ギタパソ(PowerShellうぴゃー)

    [System.Linq.Enumerable]::ToArray(“PowerShell”)

  • Chris Bennett

    “PowerShell”|%{for ($i=0;$i -lt $_.Length; ++$i) { $_[$i] }}

    or alternatively

    “PowerShell”|%{$s=$_;(0..($s.Length))|%{$s[$_]}}

    • Chris Bennett

      Wow, and after reading this is the same as doing
      “PowerShell”[0.."PowerShell".Length]

      Awesome!

  • proftech

    Nice scripts… great contributors…

© 2014 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: