-4

I am trying to cut the string into 6 fields with maximum length of 30 per field. Total length of the string is 173. I have created a code as below;

(
(("" + dr["MESSAGE"]).ToString().Trim().Length<30 && 
("" + dr["MESSAGE"]) != "")?(("" + dr["MESSAGE"]) + "|||||") : 
(("" + dr["MESSAGE"]).ToString().Trim().Length>150 && 
("" + dr["MESSAGE"]).ToString().Trim().Length<181)?
(("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(0,30).Trim() + "|" + 
("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(31,60).Trim() + "|" + 
("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(61,90).Trim() + "|" + 
("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(91,120).Trim() + "|"  + 
("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(121,150).Trim() + "|" + 
("" + dr["MESSAGE"]).Trim().PadRight(180,' ').Substring(151,180).Trim()) : "" + "|||||")

The code itself generates an output file but without the data. All I get is the header and the footer with the error "Index and length must refer to a location within the string" indicated at the bottom of the output file.

Would appreciate your help in resolving my issue.

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
  • Somewhere along the way you've overstepped a string's boundary. But the way you've 'written' your code leaves you( and us) with very few clues as to where the code is going wrong. Split you code into several lines and stages. The you will be able to see exactly where the error is. – Mesh Nov 29 '11 at 11:24
  • Maybe you should look into making your code more readable. – Ash Burlaczenko Nov 29 '11 at 11:25

2 Answers2

2

You said it yourself,

Total length of the string is 173.

Yet here

().PadRight(180,' ').Substring(151,180).Trim()) 

you are trying to create a substring that accesses data upto char 180.... which is longer than your string with a 173 length.

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
  • The reason why I put 180 is because the total maximum length that a user can put is 180. Since I will not know the length actual length of the string the user will input, I am padding the string with space till 180 whenever it is <180. – andie cruz Nov 29 '11 at 11:29
  • Ah good point! One possible thought I have is that it is padding the string to 180 characters but the index starts at 0 when you use substring (just thinking here i cant test this) so 0 - 179 is 180 chars. ie accessing 180 is 1 character too many – Purplegoldfish Nov 29 '11 at 11:32
  • @Purplegoldfish...Should I replace 180 with 179 then? – andie cruz Nov 29 '11 at 12:02
2

I think you are mistaken with how substring works.

The 2nd parameter is the length of the string to want to extract, not the index you want to finish at - MSDN Ref

Parameters

startIndex Type: System.Int32 The zero-based starting character position of a substring in this instance.
length Type: System.Int32 The number of characters in the substring.

You want to specify 30 for the 2nd parameter, as you want to split it into 30 character blocks.

I think this should work (but the code really needs to be tidied up!):

((dr["MESSAGE"].ToString().Trim().Length < 30 && test != "")
    ? (dr["MESSAGE"].ToString() + "|||||")
    : (dr["MESSAGE"].ToString().Trim().Length > 150 && test.Trim().Length < 181)
        ? dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(0, 30).Trim() + "|" +
            dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(30, 30).Trim() + "|" +
            dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(60, 30).Trim() + "|" +
            dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(90, 30).Trim() + "|" +
            dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(120, 30).Trim() + "|" +
            dr["MESSAGE"].ToString().Trim().PadRight(180, ' ').Substring(150, 30).Trim()
        : "|||||");

This question - Splitting a string / number every Nth Character / Number? might help you with some nicer code

Community
  • 1
  • 1
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • Thanks Richard D. Unfortunately I cannot do any coding other than per line expression. A tool has been provided by our vendor for me to generate an output file from the database as per formart I require. But the tool only allows me to do a coding similar to the one give. I have tried your code but I still get the same result. – andie cruz Nov 29 '11 at 12:01
  • @andiecruz I have tidied up the example and tested it locally and it seems to work. Can you try it again and let me know if it works for you? There seemed to be too many brackets which might have been causing problems. – Richard Dalton Nov 29 '11 at 13:56
  • @RichardD...Unfortunately the brackets has some functions inside the code of the system created by the vendor. I have tried using your code but it is still not working. – andie cruz Nov 30 '11 at 05:47
  • I'd write out a dump of typical values of dr["MESSAGE"]. And pass it through a test harness, that consists of the above code, written out more clearly. Then you can find which values are throwing, and more importantly you should be able to step through and work out exactly where the code throws. – Mesh Nov 30 '11 at 09:57