string[]相關問題!謝謝了!

easywolf 2005-03-23 09:53:41
問題:
string text1="www,eee";
string[] temp1=text1.split(',');
這樣執行后有 temp1[0]和temp[1]存在!
我要是
string text1="www";的話temp1[1]就不存在了!
我該怎樣判斷temp1[1]是否存在?
我用 if(temp1[1]!=null) 是錯的!
請您幫幫我!
給我寫出代碼謝謝了!
...全文
71 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Albert_Andrew 2005-03-23
  • 打赏
  • 举报
回复
如果只有两个string的话:
string[] temp = new string[2];
temp = text1.split(',');
if(temp[1].ToString() != "")

Ivony 2005-03-23
  • 打赏
  • 举报
回复
因为temp是数组,所以如果temp[1]不存在直接就是数组下表越界的异常。
Ivony 2005-03-23
  • 打赏
  • 举报
回复
temp.Count < 2 or temp.Count <= 1
fanruinet 2005-03-23
  • 打赏
  • 举报
回复
string text1="www,eee";
string[] temp1=text1.split(',');
if (temp1.Length > 1)
{
//temp[1]肯定存在
}
if (temp1.Length > 2)
{
//temp[2]肯定存在
}
yufenfeila 2005-03-23
  • 打赏
  • 举报
回复
temp1.Length 是数组里元素的个数
如果对temp1中的每个元素进行操作,可以:

for (int i = 0; i < temp1.Length; i++)
{
//your code
}
easywolf 2005-03-23
  • 打赏
  • 举报
回复
不行呀!和我的提示是一樣的!超出數組
temp1[1]不存在!不能用你說的方法!
Albert_Andrew 2005-03-23
  • 打赏
  • 举报
回复
忘了括号:if(temp[1].ToString() != "")
Albert_Andrew 2005-03-23
  • 打赏
  • 举报
回复
if(temp[1].ToString != "")

111,097

社区成员

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

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

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