[RESOLVED] Working with strings
My application has a textbox in which the user enters "names". I per line.
I wrote some code to get the total count of the "names" and discovered it worked properly.
However if the user accidentally presses enter again after typing in the names it will get an in accurate count because it is counting the enter spaces as a line.
-Is there a way to remove any extra enter spaces at the end of my code
so the count wont be messed up?
-Example?
Thanks
Re: [RESOLVED] Working with strings
If you want to use a textbox instead of a richtextbox, use "\r\n" instead of "\n"
Code:
public int GetNamesCount( )
{
string[ ] names = NamesForm.txtNames.Text.Split( new String[ ] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries );
int Count = 0;
foreach( string name in names )
{
Count++;
}
if( NamesForm.txtNames.Text.Length == 0 )
{
return 0;
}
else
{
return Count;
}
}
Re: [RESOLVED] Working with strings
Of course the real fix would be to split on 'Environment.NewLine'