长度不能为零vb.net

时间:2022-09-29 21:37:19

Hi is there away to detect the length of a byte before I get the error message:

您好在我收到错误消息之前检测一个字节的长度:

Length cannot be less than zero. Parameter name: length

长度不能小于零。参数名称:长度

I get the error on this line:

我在这一行得到错误:

new_username = new_username.Substring(0, new_username.IndexOf(" Joined "))

new_username = new_username.Substring(0,new_username.IndexOf(“Joined”))

I am removing the "joined" from the string I get....how can I ignore it is "joined" isnt the the data?

我从字符串中删除“加入”我得到....我怎么能忽略它是“加入”不是数据?

Thanks

3 个解决方案

#1


I would test to see what IndexOf returned before using it in this context:

在这种情况下使用它之前,我会测试看看IndexOf返回了什么:

if(new_username.IndexOf(" Joined") > 0)
{
      new_username = new_username.Substring(0, new_username.IndexOf(" Joined "))
}

#2


Try this:

new_username = new_Username.Replace(" Joined ", "")

Be warned that this will remove all occurrences of the "Joined" substring rather than just the first.

请注意,这将删除所有出现的“Joined”子字符串,而不仅仅是第一个。

#3


It looks like new_username.IndexOf(" Joined ") is returning -1 meaning the string " Joined" was not found by Substring. I would break this out into two statements:

看起来new_username.IndexOf(“Joined”)返回-1表示子串未找到字符串“Joined”。我会把它分成两个陈述:

The error you are seeing is that you are effectively making this call:

您看到的错误是您正在有效地拨打此电话:

new_username = new_username.Substring(0, -1)

#1


I would test to see what IndexOf returned before using it in this context:

在这种情况下使用它之前,我会测试看看IndexOf返回了什么:

if(new_username.IndexOf(" Joined") > 0)
{
      new_username = new_username.Substring(0, new_username.IndexOf(" Joined "))
}

#2


Try this:

new_username = new_Username.Replace(" Joined ", "")

Be warned that this will remove all occurrences of the "Joined" substring rather than just the first.

请注意,这将删除所有出现的“Joined”子字符串,而不仅仅是第一个。

#3


It looks like new_username.IndexOf(" Joined ") is returning -1 meaning the string " Joined" was not found by Substring. I would break this out into two statements:

看起来new_username.IndexOf(“Joined”)返回-1表示子串未找到字符串“Joined”。我会把它分成两个陈述:

The error you are seeing is that you are effectively making this call:

您看到的错误是您正在有效地拨打此电话:

new_username = new_username.Substring(0, -1)