header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

getting the last char of a string and convert to int
Last Post 29 Jun 2010 05:33 PM by cameronove. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
joez3User is Offline
New Member
New Member
Posts:12
Avatar

--
26 Jun 2010 12:38 AM  
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.shawUser is Offline
Site Moderator
Advanced Member
Advanced Member
Posts:670
Avatar

--
26 Jun 2010 12:51 AM  
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
bielawbUser is Offline
New Member
New Member
Posts:24
Avatar

--
26 Jun 2010 06:02 AM  
[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
GWHowarth88User is Offline
Basic Member
Basic Member
Posts:348
Avatar

--
26 Jun 2010 10: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:236
Avatar

--
28 Jun 2010 10:14 PM  
[int]"$($temp[-1])"
Cameron
joez3User is Offline
New Member
New Member
Posts:12
Avatar

--
29 Jun 2010 04:27 PM  
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:236
Avatar

--
29 Jun 2010 05:33 PM  
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
Cameron
You are not authorized to post a reply.

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