Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Count all lines except blank lines?

So I have a textbox with the text:

Hello
Whats
[blank line]
Up

and when I count the lines I get 4. But I don't want to count the blank line, So i want it to say 3...

So how would I do something like this?
Only increment the count if it isn't blank. Simple concept but is annoying to apply. I say this because buttons like Tab, Enter and Space are characters so if one is pressed the line appears to be empty but the lines are actually not blank. To get it to work you would have to check the line to see if it had an Enter character (because enter creates a new line, and if only enter is on that line then it will not contain any text.) and if it contains an enter in the line don't increment your counter and move to the next line.
Code
Dim lineCount as integer
For each line as string in textbox1.text.split(environment.newline)
if not string.isnullorempty(line) then lineCount += 1
next
messagebox.show(linecount)
(Feb 10, 2013 07:05 PM)Cody8295 Wrote:  
Code
Dim lineCount as integer
For each line as string in textbox1.text.split(environment.newline)
if not string.isnullorempty(line) then lineCount += 1
next
messagebox.show(linecount)

Textbox's Text property can't be null (not even a section of the text, that's just not going to happen), there's been discussion about that IsNullOrEmpty() function for TextBoxes.

Code
textbox1.text.split(environment.newline)

Or:
Code
TextBox1.Lines

?? 🤨
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
[Image: B7wBO0T.png]

No, the string will never be a null reference but could potentially be empty, or consist only of white spaces so I like this method in the given context.
If he doesn't need that whitespace check though it's probably better to compare against String.Empty alone though. Regardless this is all that function does:

Code
public static bool IsNullOrWhiteSpace(string value)
{
    if (value != null)
    {
        for (int i = 0; i < value.Length; i++)
        {
            if (!char.IsWhiteSpace(value[i]))
            {
                return false;
            }
        }
    }
    return true;
}

So it might not be so difficult if you were to create your own wrapper here that didn't do the null check if you were picky about optimization.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Feb 11, 2013 03:19 AM)+Bourbon Wrote:  [Image: B7wBO0T.png]

No, the string will never be a null reference but could potentially be empty, or consist only of white spaces so I like this method in the given context.

This worked perfect for me. Thank you so much man!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Cabgun JockJun 10, 2019 08:13 AM 3 149 Jun 10, 2019 08:13 AM
Last Post: HFDone
[C#] by sososeriousOct 1, 2017 12:11 AM 4 171 Oct 1, 2017 12:11 AM
Last Post: sososerious
[Java] by sp1k3677May 6, 2015 06:59 AM 7 191 May 6, 2015 06:59 AM
Last Post: Neotokyo