Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-检查数组位置是null还是空格?_C# - Fatal编程技术网

c#-检查数组位置是null还是空格?

c#-检查数组位置是null还是空格?,c#,C#,我需要检查数组的最终位置是否等于空格“”。下面的代码抛出了一个超出范围的异常,words变量通过regex模式在末尾包含一个空格 代码: .Count()返回数组中的元素数,但第一个元素索引是0,所以最后一个索引应该是单词。Count()-1.Count()返回数组中的元素数,但第一个元素索引是0,所以最后一个索引应该是单词。Count()-1 使用count()时,数组位置必须为-1。谢谢 使用count()时,数组位置必须为-1。谢谢。您也可以使用以下代码。IsNullOrWhiteSpac

我需要检查数组的最终位置是否等于空格“”。下面的代码抛出了一个超出范围的异常,words变量通过regex模式在末尾包含一个空格

代码:

.Count()
返回数组中的元素数,但第一个元素索引是0,所以最后一个索引应该是
单词。Count()-1
.Count()返回数组中的元素数,但第一个元素索引是0,所以最后一个索引应该是
单词。Count()-1

使用count()时,数组位置必须为-1。谢谢


使用count()时,数组位置必须为-1。谢谢。

您也可以使用以下代码。
IsNullOrWhiteSpace
方法检查给定字符串是否只包含null或空格字符

    string[] words = pattern.Split(input);
    int limit = words.Length;

    if(String.IsNullOrWhiteSpace(words[limit-1]))
    {
      limit-=1; //edit value of limit based on your own logic
    }

您还可以使用以下代码。
IsNullOrWhiteSpace
方法检查给定字符串是否只包含null或空格字符

    string[] words = pattern.Split(input);
    int limit = words.Length;

    if(String.IsNullOrWhiteSpace(words[limit-1]))
    {
      limit-=1; //edit value of limit based on your own logic
    }

在这种情况下,最后一个元素将等于“limit”。如果final元素==(“”),它如何编写if语句?谢谢。我在那件事上完全发呆了。干杯。在本例中,最后一个元素将等于“limit”。如果final元素==(“”),它如何编写if语句?谢谢。我在那件事上完全发呆了。干杯
    string[] words = pattern.Split(input);
    int limit = words.Length;

    if(String.IsNullOrWhiteSpace(words[limit-1]))
    {
      limit-=1; //edit value of limit based on your own logic
    }