c# c/s结构系统 startIndex 不能大于字符串长度。怎么解决。。。。。。。。

suners 2008-12-01 03:24:44
pos.GpsTime = "20" + ss[add + 10].Substring(4) + "-" + ss[add + 10].Substring(2, 2) + "-" + ss[add + 10].Substring(0, 2)
+ " " + ss[add + 2].Substring(0, 2) + ":" + ss[add + 2].Substring(2, 2) + ":" + ss[add + 2].Substring(0, 2);
DateTime dt = DateTime.Parse(pos.GpsTime);
dt = dt.AddHours(8);
pos.GpsTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
...全文
775 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
suners 2008-12-02
  • 打赏
  • 举报
回复
搞定了。。。。散分了。。
多谢大侠了。。
自己跟踪调出来了。。。
suners 2008-12-02
  • 打赏
  • 举报
回复
恩考虑下在试试。。。
止戈而立 2008-12-01
  • 打赏
  • 举报
回复
就你给出这个代码,会造成这个错误的地方实在太多太多了。。
pos.GpsTime = "20" + ss[add + 10].Substring(4) + "-" + ss[add + 10].Substring(2, 2) + "-" + ss[add + 10].Substring(0, 2)
+ " " + ss[add + 2].Substring(0, 2) + ":" + ss[add + 2].Substring(2, 2) + ":" + ss[add + 2].Substring(0, 2);


类似标有颜色的地方非常多。。不能光注意Substring方法,还要看看ss数组的下标会不会越界。。
tang_fu 2008-12-01
  • 打赏
  • 举报
回复
public string Substring(
int startIndex,
int length
)

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.

Return Value
Type: System..::.String

A String equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero.

Exceptions
Exception Condition
ArgumentOutOfRangeException startIndex plus length indicates a position not within this instance.

-or-

startIndex or length is less than zero.


你的第一个参数startIndex已经超出了字符串的长度,检查你代码里面的那些Substring方法里面的参数是否正确。上面是MSDN里面关于该函数的详细说明,请仔细看Exceptions部分。

bbbbbb888888 2008-12-01
  • 打赏
  • 举报
回复
debug搞定一切...写代码不能太懒.
suners 2008-12-01
  • 打赏
  • 举报
回复
该了怎么还是那样、。。。
得不到值 是空值
yanrabbit163 2008-12-01
  • 打赏
  • 举报
回复
楼主想要干什么呀,如果想要把一个UTC的日期转成北京时间,可以用.ToLocalTime()方法
全速前行 2008-12-01
  • 打赏
  • 举报
回复
楼上真的很牛!
bloodish 2008-12-01
  • 打赏
  • 举报
回复
假设ss[add + 10]只有2个字符
那么执行
ss[add + 10].Substring(2, 2)就会报你提到的错误了

如3楼所说,调试一下,自己动手,才能真正搞懂问题.
HDNGO 2008-12-01
  • 打赏
  • 举报
回复
DateTime.Now.Month.ToString().PadLeft(2,'0')
HDNGO 2008-12-01
  • 打赏
  • 举报
回复
如果是时间补零什么的,用PadLeft比较方便~
cuike519 2008-12-01
  • 打赏
  • 举报
回复
public string Substring(
int startIndex,
int length
)

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.

Return Value
Type: System..::.String

A String equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero.

Exceptions
Exception Condition
ArgumentOutOfRangeException startIndex plus length indicates a position not within this instance.

-or-

startIndex or length is less than zero.


你的第一个参数startIndex已经超出了字符串的长度,检查你代码里面的那些Substring方法里面的参数是否正确。上面是MSDN里面关于该函数的详细说明,请仔细看Exceptions部分。

HDNGO 2008-12-01
  • 打赏
  • 举报
回复
跟一下值哇~
cpio 2008-12-01
  • 打赏
  • 举报
回复

所以,为了不出异常,可以先判断长度够不够

但是由于你这样的代码不好进行判断,你可以在字符串后面加足够的空格(或者其它字符,然后再在结果里面替换掉),这样就不会出异常了
cpio 2008-12-01
  • 打赏
  • 举报
回复
Substring(0, 2),像这样的方法,必须保证长度大于等于2,就是说要取2长度出来,原字符串必须够2的长度

suners 2008-12-01
  • 打赏
  • 举报
回复
运行到此处就抛出异常。。。。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧