Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a problem that I am finding a string that is coming via machine and putting the values in datagrid. Machine is a note sorting machine and sometimes it gives reject notes sometimes not. My question is when it is giving rejects my code is working fine. However when it is not giving any rejects it gives some other data that i dont want i want zero to be displayed. Here is the con how can i do it.
C#
string o=@"Reject      count          value
--------------------------------";

                int a = str.IndexOf(o) + o.Length;
             //  if(
                //{
                    string p = "Total:";
                    int b = str.IndexOf(p, a);
                    string b = str.Substring(a, b-a);
                    dataGridView1.Rows[n].Cells[5].Value = str6;
Posted
Updated 12-Jun-15 6:42am
v2
Comments
Richard Deeming 12-Jun-15 12:43pm    
That's the second time I've seen a question from you which contains a "netseer" script block at the bottom.

If you're trying to inject third-party adverts into the site, it's not going to work.

If you didn't type the script block, then you have malware installed on your computer.

1 solution

While your code example seems hard to read and could be more clear - I think this is what you are after:

C#
string o=@"Reject      count          value";

// Changed this Code
// Gets the index of string O in the string
// int a = str.IndexOf(o) + o.Length;

int a = str.IndexOf(o);

// End Changed Code

// Added Code check if the string o=@"Reject      count          value" is even found
if(a != -1)
{

// End Added Code


// Changed Code -
// Adding th elenght of your string to int a [Index + String o length ]

a += o.Length;

// End Changed Code


string p = "Total:";
// Starting at point a in string str
// get index of string P
int b = str.IndexOf(p, a);

// String b? b is defined as int already - lets call it best
string best = str.Substring(a, b-a);
dataGridView1.Rows[n].Cells[5].Value = str;


// Added Code
}
Else  // o "Reject      count          value" was not found
{
// Do what you need to for a non reject - set the value to 0
str = 0;

}
// End Added Code
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900