c# - Substring causing index and length error -


i have section in code run check see if item spanish item or english item. using logic old vb.net application.

public int spanish_item() {     int = 0;     object j = 0;     int k = 0;     string ss = null;     string sp_item = null;     sp_item = txtitem.text.trim();     k = 0;     (i = 1; <= 15; i++)     {         ss =  sp_item.substring(i, 2);         if (ss == "xx")         {             k = 1;             = 16;         }     }     return k; } 

the following code loops around error message :

ex.message "index , length must refer location within string.\r\nparameter name: length" string

please help!!!

you go 1 15 - if (trimmed) text of txtitem.text shorter 15 chars you'll exception.

you should use length-2 of sp_item upper bound avoid error. also, instead of setting i = 16 should use break stop loop.

however, think algorithm written instead of for loop:

if (sp_item.indexof("xx")>=1) {     k=1; } 

Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -