![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Getting substring to react sanely to end of index? Richard Price wrote:
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 | ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Getting substring to react sanely to end of index?
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 | ||||||||||||
| | #4 (permalink) | ||||||||||||
| 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
| ||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Getting substring to react sanely to end of index? Richard Price wrote:
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 | ||||||||||||||||||||||||
| |
| |
![]() |
| 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 |