索引和长度必须引用一个位置字符串中 [英] Index and length must refer to a location within the string
问题描述
我试图将字符串切成30最大长度每场6个领域。字符串的总长度是173我已经创建了一个低于code;
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()) : "" + "|||||")
在code本身产生一个输出文件,但没有数据。我得到的是标头和错误的页脚索引和长度必须引用在字符串中的位置输出文件的底部表示。
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.
请问AP preciate你解决我的问题的帮助。
Would appreciate your help in resolving my issue.
推荐答案
我想你是误会着如何子的作品。
I think you are mistaken with how substring works.
第2个参数是字符串的长度要提取,不是你想在完成指标 - 的 MSDN参考
The 2nd parameter is the length of the string to want to extract, not the index you want to finish at - MSDN Ref
参数
的startIndex类型:System.Int32
从零开始的起始字符
在这种情况下的子串的位置。结果
长类型:System.Int32
字符的子字符串中的数量。
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.
您要为第二个参数指定30,只要你想它分割成30字符块。
You want to specify 30 for the 2nd parameter, as you want to split it into 30 character blocks.
我觉得这应该工作(但code真正需要收拾!):
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()
: "|||||");
这个问题 - <一个href=\"http://stackoverflow.com/questions/4133377/splitting-a-string-number-every-nth-character-number\">Splitting字符串/数字每隔N个字符/数字?可能会帮助你一些更好的code
This question - Splitting a string / number every Nth Character / Number? might help you with some nicer code
这篇关于索引和长度必须引用一个位置字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!