2

i have created web application and using textbox and it can contains multiple line of data becoz i have set its textmode property is multiline. my problem is that i want to check each line contain data or not so i using count variable which count how many line contain data.

string[] data;
int cntindex;   
data = txt_invoicenumber.Text.ToString().Split("\n".ToCharArray());
cntindex = data.Length;
for (j = 0; j < cntindex; j++)
{
    if (data[j]!="")  
    {
        inv_count++;            
    }        
}

Its not working. Please help me.

1
  • I'd recommend using string.IsNullOrEmpty(). Commented Jun 22, 2010 at 11:24

4 Answers 4

1

I guess this is because new line is \r\n so there is a '\r' also on empty lines.
Change the if statement to:

if (data[j].Trim().Length != 0)
Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, You don't need to ToString() the .Text property as it is already a string.

try this

string[] lines = txt_invoicenumber.Text.Split(Environment.NewLine);
int lineCount = 0;
foreach(string line in lines)
{
    if(!string.IsNullOrEmpty(line))
    {
        lineCount ++;
        this.ProcessLine(line);
    }
}

Comments

0
var lb = new String[] { "\r\n" };    
var lines = txt_invoicenumber.Text.Split(lb, StringSplitOptions.None).Length;

This will count empty lines too. If you don't want to count empty lines, use the StringSplitOptions.RemoveEmptyEntries value.

Don't count 100% on "\r\n" if you have little control over your environment though.

Comments

0

This is the answer I came up with.

String[] lines = TextBox1.Text.Split(new Char[] { '\r', '\n' },
                                     StringSplitOptions.RemoveEmptyEntries);
Int32 validLineCount = lines.Length;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.