Windows Vista Forums

Getting substring to react sanely to end of index?
  1. #1


    Richard Price Guest

    Getting substring to react sanely to end of index?

    Hi,

    Is it possible to somehow force substring to just return what it can
    if its asked to substring more of a string than there is remaining.

    Eg 'this is a string' is 16 characters long, imagine its assigned to
    variable $a.



    $a.substring(0,4) would return 'this '

    $a.substring(10,3) would return 'str'

    $a.substring(14,4) would throw an exception because it causes
    substring to seek beyond the string boundary. Ideally what would
    happen is it returns 'ng'. A substring referencing a range totally
    outside of teh boundary is fine to throw an exception, that can be
    dealt with.

    Am I just barking up the wrong tree? Should I be dealing with this
    programmatically?

    Cheers
    Richard

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Getting substring to react sanely to end of index?

    Richard Price wrote:

    > Hi,
    >
    > Is it possible to somehow force substring to just return what it can
    > if its asked to substring more of a string than there is remaining.
    >
    > Eg 'this is a string' is 16 characters long, imagine its assigned to
    > variable $a.
    >
    > $a.substring(0,4) would return 'this '
    >
    > $a.substring(10,3) would return 'str'
    >
    > $a.substring(14,4) would throw an exception because it causes
    > substring to seek beyond the string boundary. Ideally what would
    > happen is it returns 'ng'. A substring referencing a range totally
    > outside of teh boundary is fine to throw an exception, that can be
    > dealt with.
    >
    > Am I just barking up the wrong tree? Should I be dealing with this
    > programmatically?
    >
    > Cheers
    > Richard
    $a.substring(14) will get the remainder of the string. That what you
    were looking for or am I off?

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  3. #3


    Richard Price Guest

    Re: Getting substring to react sanely to end of index?


    >
    > $a.substring(14) will get the remainder of the string. *That what you
    > were looking for or am I off?
    Close but no milkshake - basically I want a way to be able to get a
    substring without necessarily knowing that the end of the string is
    near - I'm dealing with variable length records, where I can be sure
    of the maximum length, but not that the actual length is always the
    maximum length, so I hit the end of the string unexpectedly.

    I've written a very basic 'sane' get-substring function which does
    what I wish -

    ## Return a substring (sanely)
    function Get-SubString( [string] $value, [int] $startOfRange, [int]
    $numOfChars )
    {
    ## How long is the string
    $strLength = $value.length

    ## Last index in string (index starts from 0, string length starts
    from 1)
    $strLastIndex = ($strLength - 1)

    ## Get number of characters in range (add one to include current
    character)
    $rangeLength = ($strLastIndex - $startOfRange) + 1

    ## Check to ensure number of characters required
    ## does not exceed characters available in string
    if ( $numOfChars -gt $rangeLength )
    {
    ## return what we can
    $numOfChars = $rangeLength
    }

    ## Check to see if the first index is within string
    if ( $startOfRange -gt $strLastIndex )
    {
    throw "Start index exceeds end boundary"
    }
    if ( $startOfRange -lt 0 )
    {
    throw "Start index exceeds start boundary"
    }

    ## Do the sub string!
    $value.substring($startOfRange,$numOfChars)
    }

    Passing an index either greater than or less than the string length
    (-1 or 25 in the original string) throws an exception that can be
    handled, otherwise it returns either exactly the number of characters
    asked for, or as many as left in the string.

    Maybe that gives a better idea of what I was asking?

    Cheers
    Richard

      My System SpecsSystem Spec

  4. #4


    Shay Levi Guest

    Re: Getting substring to react sanely to end of index?


    Hi Richard,


    You can right pad $a to the total of 14+4:

    PS 55> ("{0,-18}" -f $a).substring(14,4)
    ng

    but this returns additional two spaces on the right side:

    PS 57> ("{0,-18}" -f $a).substring(14,4).length
    4


    So, add a trim() call to remove the trailing spaces:


    PS 56> ("{0,-18}" -f $a).substring(14,4).trim()
    ng



    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > Hi,
    >
    > Is it possible to somehow force substring to just return what it can
    > if its asked to substring more of a string than there is remaining.
    >
    > Eg 'this is a string' is 16 characters long, imagine its assigned to
    > variable $a.
    >
    > $a.substring(0,4) would return 'this '
    >
    > $a.substring(10,3) would return 'str'
    >
    > $a.substring(14,4) would throw an exception because it causes
    > substring to seek beyond the string boundary. Ideally what would
    > happen is it returns 'ng'. A substring referencing a range totally
    > outside of teh boundary is fine to throw an exception, that can be
    > dealt with.
    >
    > Am I just barking up the wrong tree? Should I be dealing with this
    > programmatically?
    >
    > Cheers
    > Richard


      My System SpecsSystem Spec

  5. #5


    Marco Shaw [MVP] Guest

    Re: Getting substring to react sanely to end of index?

    Richard Price wrote:

    >> $a.substring(14) will get the remainder of the string. That what you
    >> were looking for or am I off?
    >
    > Close but no milkshake - basically I want a way to be able to get a
    > substring without necessarily knowing that the end of the string is
    > near - I'm dealing with variable length records, where I can be sure
    > of the maximum length, but not that the actual length is always the
    > maximum length, so I hit the end of the string unexpectedly.
    >
    > I've written a very basic 'sane' get-substring function which does
    > what I wish -
    I think I got you now...

    You also want to handle stuff like this also:

    PSH>$string
    this is a string
    PSH>$string.substring(20)
    Exception calling "Substring" with "1" argument(s): "startIndex cannot
    be larger than length of string.
    Parameter name: startIndex"
    At line:1 char:18
    + $string.substring <<<< (20)
    PSH>

    I don't see any other way myself at this time. You could spend time
    trying to determine if an exception occured, but I think you'er way is
    cleaner.

    Marco

      My System SpecsSystem Spec

Getting substring to react sanely to end of index? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
substring and error - why? mrkramer VB Script 10 19 Apr 2010
Keyboard slow to react when switching users dhub General Discussion 0 25 Jan 2009
WEP Key Index =?Utf-8?B?R2lnYWJ5dGU2OQ==?= Vista installation & setup 2 12 May 2008
Select-string excluding a substring Altraf PowerShell 5 23 Nov 2007
WEP Key Index =?Utf-8?B?R2lnYWJ5dGU2OQ==?= Vista installation & setup 0 11 Jul 2006