Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Please could someone help me find the greatest number of a string? Here's the code:

C#
if (dt.Rows.Count > 0)
                   {
                       int max = 0;
                       foreach (DataRow dr in dt.Rows)
                       {
                           bookNo = dr.ItemArray[0].ToString();
                           int i = Convert.ToInt32(bookNo.Substring(bookNo.Length - 1));
                           if (i > max)
                           {

                           }
                       }


Thanks
Posted

C#
if (i > max)
{
    max = i;
}
 
Share this answer
 
Comments
Rahul VB 14-Jan-14 11:23am    
very correct.
CHill60 14-Jan-14 19:28pm    
Love it ... a max solution from Maxxx :-)
Just write

C#
max = i;


Inside the "if" statement. then at the end of your loop, max will contain the maximum value.
 
Share this answer
 
place your all data in generic List<int> & call max method of list.

XML
List<int> iList = new List<int>();
         //Here add values into iList
         int iMax = iList.Max();
 
Share this answer
 
C#
if (dt.Rows.Count > 0)
                   {
                       int max = 0;
                       foreach (DataRow dr in dt.Rows)
                       {
                           bookNo = dr.ItemArray[0].ToString();
                           int i = Convert.ToInt32(bookNo.Substring(bookNo.Length - 1));
                           if (i > max)
                           {
                              max=i;
                           }
                       }
 
Share this answer
 
Modify the below code according to your requirement.This piece of code worked for me.

C#
int length = 0;
           string longestTelephone = null;

           for (int i = 0; i < allTelephone.Count; i++)
           {

               if (length < allTelephone[i].Length)
               {
                   length = allTelephone[i].Length;
                   longestTelephone = allTelephone[i];
               }
           }

Hope this may help.
 
Share this answer
 

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