Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Getting substring to react sanely to end of index?

Closed Thread
 
Thread Tools Display Modes
Old 05-15-2008   #1 (permalink)
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
Old 05-15-2008   #2 (permalink)
Marco Shaw [MVP]
Guest


 

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

Richard Price wrote:
Quote:

> 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
Old 05-15-2008   #3 (permalink)
Richard Price
Guest


 

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

Quote:

>
> $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
Old 05-15-2008   #4 (permalink)
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
Quote:

> 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

Old 05-15-2008   #5 (permalink)
Marco Shaw [MVP]
Guest


 

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

Richard Price wrote:
Quote:
Quote:

>> $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
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
WEP Key Index =?Utf-8?B?R2lnYWJ5dGU2OQ==?= Vista installation & setup 2 05-12-2008 02:00 PM
Select-string excluding a substring Altraf PowerShell 5 11-23-2007 12:41 PM
Index of pdf files Blanca Vista file management 6 05-09-2007 12:24 PM
WEP Key Index =?Utf-8?B?R2lnYWJ5dGU2OQ==?= Vista installation & setup 0 07-11-2006 06:33 PM
add to index... Hao Vista file management 0 06-22-2006 08:23 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50