c# - Count numbers of \r\r in a string -
this question has answer here:
i want count, how have \r\r
in string variable.
for example:
string samplestring = "9zogl22n\r\r\nv4bv79gy\r\r\nkaz73ji8\r\r\nxw0w91qq\r\r\ns05jxqxx\r\r\nw08qsxh0\r\r\nuyggbaec\r\r\nu2izr6y6\r\r\n106iha5t\r";
the result in example 8.
you can write simple extension method that:
public static int linefeedcount(this string source) { var chars = source.tochararray(); bool found = false; int counter = 0; int count = 0; foreach(var ch in chars) { if (ch == '\r') found = true; else found = false; if (found) counter++; else counter = 0; if(counter != 0 && counter%2 == 0) count++; } return count; }
then use it:
int count = inputstring.linefeedcount();
Comments
Post a Comment