c# - string.IndexOf search for whole word match -



c# - string.IndexOf search for whole word match -

i seeking way search string exact match or whole word match. regex.match , regex.ismatch don't seem me want be. consider next scenario:

namespace test { class programme { static void main(string[] args) { string str = "subtotal 34.37 taxation total 37.43"; int indx = str.indexof("total"); string amount = str.substring(indx + "total".length, 10); string stramount = regex.replace(amount, "[^.0-9]", ""); console.writeline(stramount); console.writeline("press key continue..."); console.readkey(); } } }

the output of above code is:

// 34.37 // press key continue...

the problem is, don't want subtotal, indexof finds first occurrence of word "total" in "subtotal" yields wrong value of 34.37.

so question is, there way forcefulness indexof find exact match or there way forcefulness exact whole word match can find index of exact match , perform useful function it. regex.ismatch , regex.match are, far can tell, boolean searches. in case, isn't plenty know exact match exists. need know exists in string.

any advice appreciated.

you can utilize regex

string str = "subtotal 34.37 taxation total 37.43"; var indx = regex.match(str, @"\wtotal\w").index; // 18

c# regex string substring indexof

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -