locked
Start Index Error RRS feed

  • Question

  • I have a really quick question regarding this error:

    Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string.

    Well how in the world cannot it not be?  That's not my question by the way.  I just want to know if there is something I might be doing wrong or if it can be corrected? 

    I get my desired output when my script is ran, I just get a little OCD about cleaning things up, hence getting rid of scary red text.

    So, if I have something like this

    [PSCustomObject]@{
                Header1 = $_.SubString(1, 10)
                Header2 = $_.SubString(11, 20)
                Header3 = $_.SubString(37, 15)

    and a line of text that is:

    "Header1Info    Header2Info                            Header3Info"

    The startindex of each column is going to be bigger than the length of the values at some point, correct?  Is there a better way to address this, or do I just have to live with the errors?  Like I said before, the output I want is correct, I just don't like seeing the error messages. 


    sharpkey

    Thursday, May 5, 2016 7:48 PM

Answers

  • You have to filter blank lines and you cannot count tabs as spaces.

    String index stats on zero (0).

    Given what you posted this is what works on that one line:

    PS D:\scripts> @("Header1Info    Header2Info                            Header3Info")|
    >> %{
    >> $_.SubString(0, 14)
    >> $_.SubString(15, 20)
    >> $_.SubString(54, 11)
    >> }
    Header1Info
    Header2Info
    Header3Info
    PS D:\scripts>
    


    \_(ツ)_/

    • Marked as answer by SharpKey Friday, May 6, 2016 1:41 AM
    Thursday, May 5, 2016 9:21 PM

All replies

  • You have to filter blank lines and you cannot count tabs as spaces.

    String index stats on zero (0).

    Given what you posted this is what works on that one line:

    PS D:\scripts> @("Header1Info    Header2Info                            Header3Info")|
    >> %{
    >> $_.SubString(0, 14)
    >> $_.SubString(15, 20)
    >> $_.SubString(54, 11)
    >> }
    Header1Info
    Header2Info
    Header3Info
    PS D:\scripts>
    


    \_(ツ)_/

    • Marked as answer by SharpKey Friday, May 6, 2016 1:41 AM
    Thursday, May 5, 2016 9:21 PM
  • But if I input some info into a text file under those header names, and use those same substring values, as I didn't change the spacing, I still get the same error as above.  I just pasted the original string to a text file and entered random stuff under each.  It's not a big deal, I get the desired output either way, but having an error regardless if something works, just bothers me.  I can live with it though.

    $inputFile = Get-Content c:\pathtofile\test.txt

    $objects = $inputFile[2..($inputFile.Count - 1)] |
        ForEach-Object{
            [PSCustomObject]@{
            Header1 =$_.SubString(0, 14)
            Header2 =$_.SubString(15, 20)
            Header3 =$_.SubString(54, 11)
            
                
            }
        }

    $objects | format-table

    This is actually what you helped me with in an earlier thread JRV, and it works great in my script, thank you again.


    sharpkey

    Friday, May 6, 2016 1:14 AM
  • Well, never mind, I just solved the problem.  I don't know why it would matter about empty lines, but I just added ?{$_.trim() -ne ""} to the script and it got rid of the errors.

    sharpkey

    Friday, May 6, 2016 1:27 AM
  • Well, never mind, I just solved the problem.  I don't know why it would matter about empty lines, but I just added ?{$_.trim() -ne ""} to the script and it got rid of the errors.

    sharpkey

    What did I say in my post? "You have to filter blank lines and you cannot count tabs as spaces."


    \_(ツ)_/

    Friday, May 6, 2016 1:30 AM
  • So, you did.  I need to read better.  But I did notice that when I apply that to my original script it does get rid of the errors, but it also deletes some of the first few lines for some reason.

    sharpkey

    Friday, May 6, 2016 1:38 AM
  • So, you did.  I need to read better.  But I did notice that when I apply that to my original script it does get rid of the errors, but it also deletes some of the first few lines for some reason.

    sharpkey

    Not possible.  You are missing something else.  Perhaps a broken file.  Ta bs in the file.

    I have been foin g this for more tan 20 years.  I am sure you are missing some critical piece of information.  Without you file none of us can say what you have happening.


    \_(ツ)_/

    Friday, May 6, 2016 1:40 AM
  • I think it's just with some of the other trimming I'm doing in the file that is the problem.  I can fix that though.  Thanks JRV. 

    sharpkey

    Friday, May 6, 2016 1:41 AM
  • One other thing to be aware of.  Some print file are "ragged".  To detect this we would do this:

    Get-Content report.txt | ?{$_.Trim()} | %{$_.Length}

    If the lines are not the same length then you have a ragged file and have to adjust the last position on every line.


    \_(ツ)_/

    Friday, May 6, 2016 1:48 AM