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

  1. if string has 360_image.jpg have return image.jpg , cutting 360_
  2. 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

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -