Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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#编译器错误_C# - Fatal编程技术网

带有子字符串的C#编译器错误

带有子字符串的C#编译器错误,c#,C#,为什么我会得到: Index and length must refer to a location within the string. Parameter name: length 当我编译此代码时: 其中的一部分: public string findFileEnding(string file) { int index1 = file.IndexOf('.'); file = file.Substring(index1, file.Le

为什么我会得到:

Index and length must refer to a location within the string.
Parameter name: length
当我编译此代码时:

其中的一部分:

    public string findFileEnding(string file)
    {
        int index1 = file.IndexOf('.');
        file = file.Substring(index1, file.Length);
        return file;
    }

谢谢;)

子字符串的第二个参数(如果存在)是子字符串的所需长度。因此,您要求的字符串长度与
文件
相同,但起始位置可能不同于0。这将使子字符串的结尾超过
文件的结尾

假设您希望从位置
index1
获取所有
文件
,您可以完全忽略第二个参数:

file = file.Substring(index1); 
要使其健壮,您需要进行更多检查:

  • 文件
    可能为
    null
  • IndexOf
    的返回值可以是
    -1
    。如果
    文件
    不包含点,则会发生这种情况

  • 子字符串
    的第二个参数(如果存在)是所需的子字符串长度。因此,您要求的字符串长度与
    文件
    相同,但起始位置可能不同于0。这将使子字符串的结尾超过
    文件的结尾

    假设您希望从位置
    index1
    获取所有
    文件
    ,您可以完全忽略第二个参数:

    file = file.Substring(index1); 
    
    要使其健壮,您需要进行更多检查:

  • 文件
    可能为
    null
  • IndexOf
    的返回值可以是
    -1
    。如果
    文件
    不包含点,则会发生这种情况

  • 这不是编译器错误,而是运行时错误

    注意
    String.Substring(int,int)
    的文档:

    从此实例检索子字符串。子字符串从指定的字符位置[
    startIndex
    ]开始,并具有指定的长度[
    length
    ]

    因此子字符串将具有指定的长度。因此,必须有足够的字符从
    startIndex
    开始,才能返回指定长度的子字符串。因此,必须满足以下不等式才能使
    String.Substring
    String
    的实例
    s
    上成功:

    startIndex >= 0
    length >= 0
    length > 0 implies startIndex + length <= s.Length
    
    在这里,唯一的限制是

    startIndex>= 0
    startIndex < s.Length
    
    startIndex>=0
    startIndex
    这不是编译器错误,而是运行时错误

    注意
    String.Substring(int,int)
    的文档:

    从此实例检索子字符串。子字符串从指定的字符位置[
    startIndex
    ]开始,并具有指定的长度[
    length
    ]

    因此子字符串将具有指定的长度。因此,必须有足够的字符从
    startIndex
    开始,才能返回指定长度的子字符串。因此,必须满足以下不等式才能使
    String.Substring
    String
    的实例
    s
    上成功:

    startIndex >= 0
    length >= 0
    length > 0 implies startIndex + length <= s.Length
    
    在这里,唯一的限制是

    startIndex>= 0
    startIndex < s.Length
    
    startIndex>=0
    startIndex
    您可能希望执行以下操作:

    public string FindFileEnding(string file)
    {
        if (string.IsNullOrEmpty(file))
        {
            // Either throw exception or handle the file here
            throw new ArgumentNullException();
        }
        try
        {
            return file.Substring(file.LastIndexOf('.'));
        }
        catch (Exception ex)
        {
            // Handle the exception here if you want, or throw it to the calling method
            throw ex;
        }
    }
    

    您可能希望执行以下操作:

    public string FindFileEnding(string file)
    {
        if (string.IsNullOrEmpty(file))
        {
            // Either throw exception or handle the file here
            throw new ArgumentNullException();
        }
        try
        {
            return file.Substring(file.LastIndexOf('.'));
        }
        catch (Exception ex)
        {
            // Handle the exception here if you want, or throw it to the calling method
            throw ex;
        }
    }
    

    我认为有一个机会
    Path.GetExtension
    可能是OP想要的

    请注意,它返回的扩展名为
    ,类似于
    .exe


    我认为存在一种可能性
    路径。GetExtension
    可能是OP想要的

    请注意,它返回的扩展名为
    ,类似于
    .exe


    没有检查index1是否大于-1(字符串中甚至有一个.)。如果你做了一个带有/index:-1的子字符串,它也会抛出这个错误…这不是你问题的答案,但是找到文件扩展名的更可靠的方法是使用Path类:@Rikon相同的错误,但有不同的消息…要扩展@coreyogurn的注释,你需要查看这个方法。没有检查index1是否大于-1(绳子上连一个.)。如果您使用子字符串w/index:-1,它也会抛出该错误…这不是您问题的答案,但查找文件扩展名的更可靠方法是使用Path类:@Rikon相同的错误,但带有不同的消息…要扩展@coreyogurn的注释,您需要查看该方法。可能需要提及的是,OP还应该检查
    IndexOf
    returning-1。可能需要提到的是,OP还应该检查
    IndexOf
    returning-1。我猜您的意思是
    string.IsNullOrEmpty(文件)
    对于您来说,第一条if语句是无效的,因为除非您定义了自定义扩展方法。您还应该检查
    LastIndexOf
    的返回值。重新引用该异常是没有意义的。@code裸体感谢您的代码检查,仅在浏览器中很难做到。是否有网站为您验证C#不是吗这是一个完整的答案,只是一个起点。如果最后一个索引无效,那么您必须在之前或之后处理它。我最初使用的是
    int index=file.LastIndexOf(')。)
    但是删除了它,因为没有说明他们希望如何处理错误或错误值。我不知道有任何网站这样做,但也有一些。我想你的意思是
    string.IsNullOrEmpty(文件)
    对于您来说,第一条if语句是无效的,因为除非您定义了自定义扩展方法。您还应该检查
    LastIndexOf
    的返回值。重新引用该异常是没有意义的。@code裸体感谢您的代码检查,仅在浏览器中很难做到。是否有网站为您验证C#不是吗这是一个完整的答案,只是一个起点。如果最后一个索引无效,那么您必须在之前或之后处理它。我最初是用
    int index=file.LastIndexOf('.')处理它的,但删除了它,因为没有说明他们希望如何处理错误或坏值。我不知道有任何错误