我在C#中实现了一个代码,我把这个字符串//Comment
传递给了函数。为什么函数返回true?
bool function(string buf){
// split buffer from "//" and avoid from comment
string[] lineSplit = buf.Split(new string[] { "//" }, StringSplitOptions.None);
// split part of string from space and tab, and put into buffer
if (lineSplit[0] != null)
{
return true;
}
return false;
}
请帮帮我。
答案 0 :(得分:3)
如果分隔符出现在字符串的开头,则字符串分割方法将第一个元素设置为String.Empty
。你可以阅读它here
您可能希望将检查null的if语句更改为以下内容:
bool function(string buf){
// split buffer from "//" and avoid from comment
string[] lineSplit = buf.Split(new string[] { "//" }, StringSplitOptions.None);
// split part of string from space and tab, and put into buffer
if (lineSplit[0] != string.Empty)
{
return true;
}
return false;
}
答案 1 :(得分:2)
public bool function(string buf)
{
string[] lineSplit = buf.Split(new string[] { "//" }, StringSplitOptions.None);
if (lineSplit[0] != string.Empty)
{
return true;
}
return false;
}