|
-
October 21st, 2008, 01:17 PM
#1
[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
-
October 21st, 2008, 01:37 PM
#2
Re: Working with strings
When you count lines, don't just count enters.
First, get string from textbox. Then split it by enter. the length of string array would be the number of lines.
Code:
string str = richTextBox1.Text;
string[] strs = str.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
int countOfLines = strs.Length;
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
October 21st, 2008, 02:31 PM
#3
Re: Working with strings
 Originally Posted by jasonli
When you count lines, don't just count enters.
First, get string from textbox. Then split it by enter. the length of string array would be the number of lines.
Code:
string str = richTextBox1.Text;
string[] strs = str.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
int countOfLines = strs.Length;
That won't work because the enter spaces are counted as "\n" and using "StringSplitOptions.RemoveEmptyEntries" wont remove the extra enterspaces because it sees them as "\n" and won't remove them up to the current text.
-
October 21st, 2008, 02:46 PM
#4
Re: Working with strings
Did you try this?
If it doesn't work, please post the source code you are using, let's see why.
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
October 21st, 2008, 02:52 PM
#5
Re: Working with strings
Here you go.
Code:
public int GetNamesCount( )
{
string[ ] names = NamesForm.txtNames.Text.Split( new String[ ] { "\n" }, StringSplitOptions.RemoveEmptyEntries );
int Count = 0;
foreach( string name in names )
{
Count++;
}
if( NamesForm.txtNames.Text.Length == 0 )
{
return 0;
}
else
{
return Count;
}
}
-
October 21st, 2008, 03:15 PM
#6
Re: Working with strings
I don't know if you use RichTextBox control or not. I typed lots of lines and lots of enters in RichTextBox. It did work.
In your function, just return length of array "names".
Did you type "\n" instead of Enter?
I'm totally confused.
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
October 21st, 2008, 03:34 PM
#7
Re: Working with strings
 Originally Posted by jasonli
I don't know if you use RichTextBox control or not. I typed lots of lines and lots of enters in RichTextBox. It did work.
In your function, just return length of array "names".
Did you type "\n" instead of Enter?
I'm totally confused.
Sorry man, didn't realize it had to be a rich text box.
Thanks for your help. Its working
-
October 21st, 2008, 03:35 PM
#8
Re: Working with strings
 Originally Posted by jasonli
I don't know if you use RichTextBox control or not. I typed lots of lines and lots of enters in RichTextBox. It did work.
In your function, just return length of array "names".
Did you type "\n" instead of Enter?
I'm totally confused.
I will agree with Jason here. I tested it, and it worked with a RichTextBox. However, it did NOT work with a textbox with the "multiline" property set to true.
Pale, are you sure you are using a RichTextBox?
EDITED: you answered before I could post.
-
October 26th, 2008, 09:10 AM
#9
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;
}
}
Sincerely,
Martin Svendsen
-
October 26th, 2008, 09:20 AM
#10
Re: [RESOLVED] Working with strings
Of course the real fix would be to split on 'Environment.NewLine'
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|