Hack Forums
[.NET] Count all lines except blank lines? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Count all lines except blank lines? (/showthread.php?tid=3249105)



Count all lines except blank lines? - Its Cheefin Again - Feb 10, 2013

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?


* - XvI Beta IvX - Feb 10, 2013

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.


* - Cody8295 - Feb 10, 2013

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)



* - 0xDEAD10CC - Feb 11, 2013

(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

?? 🤨


* - +Bourbon - Feb 11, 2013

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


* - 0xDEAD10CC - Feb 11, 2013

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.


* - Its Cheefin Again - Feb 11, 2013

(Feb 11, 2013 03:19 AM)ByteBlast 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!