c# - how to check the condition in asp.net for string array -
i have check condition in asp.net string array
the conditions can either have 2 values 360__image.jpg , image.jpg.
have return correct value condition
- if string has 360_image.jpg have return image.jpg , cutting 360_
- if string image.jpg have return same image.jpg
code
public string splitstring(string str) { string[] filename = str.split('_'); if (filename[2] != "") { return filename[2]; } else { return filename[0]; } } the problem above code getting error
index outside bounds of array
you should check length before accessing element array, why getting exception, since split resulted in array of 2 elements.
not sure requirement think can simplify method as:
public string splitstring(string str) { if (str.contains("_")) //or check 360__ return str.substring(str.lastindexof('_') + 1); else return str; }
Comments
Post a Comment