索引和计数必须引用字符串中的位置。 [英] Index and count must refer to a location within the string.

查看:449
本文介绍了索引和计数必须引用字符串中的位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

long Count = 0;
       int Company = 0;
       int ITTab = 0;
       if (!string.IsNullOrEmpty(userid))
       {
           string[] strTabTypes = tabtype.Remove(0, 1).Split(',');
           List<int> lstInt = new List<int>();
           for (int i = 0; i < strTabTypes.Length; i++)
               lstInt.Add(Convert.ToInt32(strTabTypes[i]));

           if (lstInt.Any(x => x == 7)) Company = 1;
           if (lstInt.Any(x => x == 11)) ITTab = 1;









这就是我的代码是什么样的建议,因为我做错了什么?我已经google了问题,但似乎无法找到一个有效的解决方案。





Thats what my code is like any suggestions as what i'm doing wrong? I've googled the problem however can't seem to find a working solution.

推荐答案

检查你的输入:string.Replace方法将抛出该异常,如果你有一个空字符串,因为没有任何东西可以删除一个字符。
Check your input: The string.Replace method will throw that exception if you have an empty string as there is nothing from which to remove a character.


这个问题似乎是通过这一行生成的,因为你调用的函数需要在你的字符串中至少有一个字符,



This problems seems to be generated through this line as the function you're calling requires to have atleast one character in your string,

tabtype.Remove(0, 1);





您正在检查if else块中的userid变量。您还应该添加一个条件以确保tabtype也不是null,以便最小化此错误。所以,你的代码可能就像这样一个





You're checking for the userid variable in your if else block. You should also add a condition to make sure that the tabtype is also not a null, in order to minimize this error. So, your code might be like this one

if (!string.IsNullOrEmpty(userid) && tabtype != null) {
   // here write the code of your function
}





It总是更好地检查任何空值,或者在字符串上运行任何需要非空和冗长字符串的方法之前的字符串长度(冗长我的意思是,长度在起始索引和结束索引之间的字符串对于方法)。



有关 String.Remove() [ ^ ]方法,阅读MSDN文档。



It is always better to check for any null values, or the length of the strings before running any method on the string which require a non-null and lengthy string (by lengthy I mean, the string whose length is in between the starting and ending index for the method).

For more on String.Remove()[^] method, read the MSDN document.


这篇关于索引和计数必须引用字符串中的位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆