Counting New Lines in a String
-
I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.
-
I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.
Check out the
StringReader
class. You could do something like:public static int CountLinesInString(string str) { System.IO.StringReader strR = new System.IO.StringReader(str); string temp = strR.ReadLine(); int count = 0; while(temp != null) { count++; temp = strR.ReadLine(); } return count; }
This would count the lines in the string. But this might not be exactly what you are looking for if you want an exact count of the character '\n'. Hope this helps, Nathan --------------------------- Hmmm... what's a signature? -
Check out the
StringReader
class. You could do something like:public static int CountLinesInString(string str) { System.IO.StringReader strR = new System.IO.StringReader(str); string temp = strR.ReadLine(); int count = 0; while(temp != null) { count++; temp = strR.ReadLine(); } return count; }
This would count the lines in the string. But this might not be exactly what you are looking for if you want an exact count of the character '\n'. Hope this helps, Nathan --------------------------- Hmmm... what's a signature? -
Actually the StringReader class will work better for what I want to do. Thanks for pointing me to it.
No problem... I am glad I could help. :) --------------------------- Hmmm... what's a signature?
-
I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.
str.Split('\n').Length (-1 : optional depends what u want) (and you have the lines too) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.
I'd say keep it simple and direct:
public int CountNewLines(string s)
{
int result = 0;
foreach (char c in s)
{
if (c == '\n') result++;
}
return result;
}Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
I'd say keep it simple and direct:
public int CountNewLines(string s)
{
int result = 0;
foreach (char c in s)
{
if (c == '\n') result++;
}
return result;
}Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
C#:
private void button6_Click(object sender, System.EventArgs e)
{
int x="some\nstuff\nwith\nnewlines\nin".Split('\n').getupperbound(0); MessageBox.Show(x.ToString());
}
that gives you the amount of \n in the string :)
-
C#:
private void button6_Click(object sender, System.EventArgs e)
{
int x="some\nstuff\nwith\nnewlines\nin".Split('\n').getupperbound(0); MessageBox.Show(x.ToString());
}
that gives you the amount of \n in the string :)
Ahh, akin to using the SUV to get the mail ;) Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.