header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Substring with variables
Last Post 11 Aug 2010 05:43 AM by Marco Shaw (MVP). 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SylnoisUser is Offline
New Member
New Member
Posts:11
Avatar

--
11 Aug 2010 04:56 AM
    Hey guys
    Have some trubbles.
    Why this donesn't work:

    $Ip = $_
    $i = 0
    do{
    $i = $i +1
    }while($Ip.substring($i, $i+1) -ne ';')

    Heres the error:

    Exception calling "Substring" with "2" argument(s): "Index and
    length must refer to a location within the string.
    Parameter name: length"
    At C:\Users\username\Desktop\Powershell\hallo.ps1:29 char:23
    + }while($Ip.substring <<<< ($i, $i+1) -ne ';')
    + CategoryInfo : NotSpecified: (:) [],
    MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

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

    --
    11 Aug 2010 05:03 AM
    I think what has you here, is there's a common misconception on what substring() does. I even forget...

    SubString(starting_position,number_of_positions_to_read)

    So, substring(1,1), gets the starting starting in position 1, and gets the following character only (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
    SylnoisUser is Offline
    New Member
    New Member
    Posts:11
    Avatar

    --
    11 Aug 2010 05:16 AM
    Ohh..
    I search a command like this:

    $Ip.COMMAND([Position_From], [Posistion_To])

    Do you know a command, that can do this.

    Sylnois
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    11 Aug 2010 05:23 AM
    Substring works, you just need to do something like this (I think):
    substring($i,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
    SylnoisUser is Offline
    New Member
    New Member
    Posts:11
    Avatar

    --
    11 Aug 2010 05:41 AM
    I just managed it.
    I'm so stupid :D
    But thanks a lot.
    Can be closed.
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    11 Aug 2010 05:43 AM
    Not stupid... We are all learning some thing or other.
    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
    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