字符串为空且不包含任何分隔符时如何处理零索引异常 [英] How handle zero index exception when string is empty and not containing any seprator
本文介绍了字符串为空且不包含任何分隔符时如何处理零索引异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用它来删除'',''及其工作正常,并且我使用str作为全局变量,但是如果字符串为空,则会出错,因此当我在aspx页面中计算全局变量时如何处理错误. />
i am using this to remove '','' and its working fine and i am using str as global variable but i get error if string is empty so how to handle error when i cal global variable in aspx page.
string _str = text.Remove(text.LastIndexOf('',''));
推荐答案
可以有多种方法来实现此目的.
你可以这样做
There could be a number of ways to do this.
You could just do this
<br />
if text.length > 0 && text.Contains('','')<br />
string _str = text.Remove(text.LastIndexOf('',''));
另一种方法是将if
语句放在同一行中
string _str = text.Length>0 && text.Contains('','')?text.Remove(text.LastIndexOf('','')):String.Empty;
Another approach could be to club the if
statement in the same line
string _str = text.Length>0 && text.Contains('','')?text.Remove(text.LastIndexOf('','')):String.Empty;
首先,您没有调用"任何变量,也没有任何全局变量.
如果您对空字符串有疑问,可以随时检查if (text == null) …
或if (string.IsNullOrEmpty(text)) …
-SA
First, you do not "call" any variables, and you don''t have any global variables.
If you have a problem with null string, you can always checkif (text == null) …
orif (string.IsNullOrEmpty(text)) …
—SA
b = dtsubstancename.Rows [i] ["Subs_name"].ToString()+," + b;
如果(b!=")
{
int backSlashIndex = b.IndexOf(,");
弱正=(backSlashIndex> == 0)? b.Remove(b.LastIndexOf('','')):b;
lblwp.Visible = true;
lblrwp.Visible = true;
}
b = dtsubstancename.Rows[i]["Subs_name"].ToString() + "," + b;
if (b != "")
{
int backSlashIndex = b.IndexOf(",");
weaklypositive = (backSlashIndex >= 0) ? b.Remove(b.LastIndexOf('','')) : b;
lblwp.Visible = true;
lblrwp.Visible = true;
}
这篇关于字符串为空且不包含任何分隔符时如何处理零索引异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文