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

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -