header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
getting the last char of a string and convert to int
Last Post 29 Jun 2010 09:33 AM by cameronove. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
zimUser is Offline
New Member
New Member
Posts:19
Avatar

--
25 Jun 2010 04:38 PM
    I have a string where the last char is an index value that I need to perform some math with. As an example the string would be: $temp = c:\temp\junk0
    To get the last char I do something like:
    $index = $temp[$temp.length-1]
    This returns 0 but when I use it with power shell says its a char. So I tried doing:
    [int]$index = $temp....
    But this returns 48 (I think thats ascii for 0). How can I get this back as a '0'

    Thanks
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    25 Jun 2010 04:51 PM
    It can only be a single number?

    Try it this way instead:
    PS>[int]$temp.substring($temp.length-1)
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    Bartek BielawskiUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    25 Jun 2010 10:02 PM
    [int][string]"This is a test0"[-1] works fine. ;)
    [-1] returns last element in array (string is char array)
    [string] will make single-char string
    [int] will return string int value
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    26 Jun 2010 02:58 AM

    A more complicated, but generic way would be to use a regular expression which extracts the trailing number of the path you are processing:

    $folders = @("C:\temp\test1", "C:\temp\test12", "C:\temp\test123", "C:\temp\t35t1234")

    $regex = [Regex]'(?< Name >.*\w[^0-9]+?)(?< Index >\d+)$' # Remove the spaces in the angle brackets

    foreach ($folder in $folders)
    {
        if ($folder -match $regex)
        {
            $Matches["Name"]
            [Int32]$Matches["Index"]
        }
    }

    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    28 Jun 2010 02:14 PM
    [int]"$($temp[-1])"
    zimUser is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    29 Jun 2010 08:27 AM
    This is odd, I tried the 1st two responses when I put them inside a script I get some strange object returned that looks like a square. If I try these from the powershell command prompt I get 0 (zero) back.
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    29 Jun 2010 09:33 AM
    This is what I get when I run your examples with my solution:

    PS H:\RFGExchmig> $temp = 'c:\temp\junk8'

    ___________________________________________________________________________________________
    PS H:\RFGExchmig> $index = [int]"$($temp[-1])"

    ___________________________________________________________________________________________
    PS H:\RFGExchmig> $index.gettype()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Int32 System.ValueType



    ___________________________________________________________________________________________
    PS H:\RFGExchmig> $index
    8
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer