string - Getting character position in c# -
string - Getting character position in c# -
this question has reply here:
how particular character position string using c# 1 replyi using next code position of "e"
string = "abcdefghijklm"; int position = 0; (int = 0; < a.length; i++) { if (a.substring(i, 1) == "e") { position = +1; break; } }
above code homecoming position = 5
,
i new c# question is fast way without loop this,
single/first occurrence
by using indexof
homecoming index of position.
using system; class programme { static void main() { // a. // input string. const string s = "abcdefghijklm"; // b. // test indexof. if (int = s.indexof("e") != -1) { console.write("string contains 'e' @ position of "+i); } console.readline(); } }
this output
"string contains 'e' @ position of 4".
for quick/better understanding:
you can larn more here or herethe indexof gets position of character (-1 returned otherwise)
multiple occurrences:if char value appears multiple times throughout string, use:
var foundindexes = new list<int>(); (int = 0; < mystr.length; i++) if (mystr[i] == 'a') foundindexes.add(i);
found here
c# string
Comments
Post a Comment