CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2008
    Posts
    161

    [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

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    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

  3. #3
    Join Date
    Mar 2008
    Posts
    161

    Re: Working with strings

    Quote 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.

  4. #4
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    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

  5. #5
    Join Date
    Mar 2008
    Posts
    161

    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;
                }
            }

  6. #6
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    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

  7. #7
    Join Date
    Mar 2008
    Posts
    161

    Re: Working with strings

    Quote 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

  8. #8
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Working with strings

    Quote 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.

  9. #9
    Join Date
    Dec 2006
    Posts
    203

    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

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    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
  •  





Click Here to Expand Forum to Full Width

Featured