C# 如何在asp.net中检查字符串数组的条件

C# 如何在asp.net中检查字符串数组的条件,c#,asp.net,conditional-statements,C#,Asp.net,Conditional Statements,我必须检查asp.net中字符串数组的条件 条件是我可以有两个值360_uimage.jpg和image.jpg 我必须从条件中返回正确的值 如果字符串有360_image.jpg,我必须只返回image.jpg和cutting 360_ 如果字符串是image.jpg,我必须返回相同的image.jpg 代码 上面代码的问题是我得到了错误 Index was outside the bounds of the array 在从数组中访问元素之前,应该检查长度,这就是为什么会出现异常,因为拆分

我必须检查asp.net中字符串数组的条件

条件是我可以有两个值360_uimage.jpgimage.jpg
我必须从条件中返回正确的值

  • 如果字符串有360_image.jpg,我必须只返回image.jpg和cutting 360_
  • 如果字符串是image.jpg,我必须返回相同的image.jpg
  • 代码 上面代码的问题是我得到了错误

    Index was outside the bounds of the array
    

    在从数组中访问元素之前,应该检查长度,这就是为什么会出现异常,因为拆分可能导致两个元素的数组

    不完全确定您的要求,但我认为您可以将您的方法简化为:

    public string splitString(string str)
    {
        if (str.Contains("_")) //or check for 360__
            return str.Substring(str.LastIndexOf('_') + 1); 
        else
            return str;
    }
    

    在从数组中访问元素之前,应该检查长度,这就是为什么会出现异常,因为拆分可能导致两个元素的数组

    不完全确定您的要求,但我认为您可以将您的方法简化为:

    public string splitString(string str)
    {
        if (str.Contains("_")) //or check for 360__
            return str.Substring(str.LastIndexOf('_') + 1); 
        else
            return str;
    }
    

    数组有2个元素,表示idex 0和1

    但您已将文件名[2]作为代码中的一部分

    第二个索引可能是错误的,这就是为什么会出现错误。可能是1

    尝试:

    public string splitString(string str)
        {
    
            string[] FileName = str.Split('_');   
    
            if (FileName[1] != "")
            {
                return FileName[1];
            }
            else
            {
                return FileName[0];
            }
        }
    

    数组有2个元素,表示idex 0和1

    但您已将文件名[2]作为代码中的一部分

    第二个索引可能是错误的,这就是为什么会出现错误。可能是1

    尝试:

    public string splitString(string str)
        {
    
            string[] FileName = str.Split('_');   
    
            if (FileName[1] != "")
            {
                return FileName[1];
            }
            else
            {
                return FileName[0];
            }
        }
    

    您可以使用
    LastIndexOf

    public string splitString(string str)
    {
        return str.Substring(str.LastIndexOf('_') + 1);
    }
    
    甚至可以使用LINQ
    Last

    public string splitString(string str)
    {
        return str.Split('_').Last();
    }
    

    您可以使用
    LastIndexOf

    public string splitString(string str)
    {
        return str.Substring(str.LastIndexOf('_') + 1);
    }
    
    甚至可以使用LINQ
    Last

    public string splitString(string str)
    {
        return str.Split('_').Last();
    }
    

    我得到的是_image001。png@Anish,尝试
    返回str.Substring(str.LastIndexOf(“”“)+1)我得到的是_image001。png@Anish,尝试
    返回str.Substring(str.LastIndexOf(“”“)+1)
    这里的字符串可以是360_image.jpg或image.jpg,因此我们需要检查上述条件answer@Anish:您实际上不需要,它仍然有效,代码也很简单。字符串可以有360_image.jpg或image.jpg,因此我们需要检查上述条件answer@Anish:实际上你不需要,它仍然有效,代码也不清楚。360_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?如果是,则用空字符串替换它。我假设“image”每次都可能是不同的字符串,但不是前缀。不清楚。360_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?如果是,则用空字符串替换它。我假设“image”每次都可以是不同的字符串,但不是前缀。