Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to generate maxid with string in asp.net.
For example,
I have the values "P0000". when i execute i want to get new id as "P0001".
Posted

This will work as long as the format of your string doesn't change (and "P9999" as upper bound).
C#
public static String nextID(String id)
{
  int n = int.Parse(id.Substring(1));
  n++;
  return id[0]+n.ToString("D4");
}
 
Share this answer
 
Comments
thatraja 11-Aug-11 8:19am    
5!
CPallini 11-Aug-11 8:44am    
Thank you.
C#
This approach will be applicable for any format of the Maxid String. For Example : D0001, ABC0001, 0001. It wont support,if the character is coming after number (0001D)

//Calling
String id = "D0001";
GenerateID(id);

//Method
public String GenerateID(String id)
{
        int pos=0;
        for (int i = id.Length; i>0 ; i--)
        {
            if (Char.IsNumber(id[i - 1]))
            {
                pos = i - 1;
            }
            else
                break;
        }
        return id.Substring(0, pos) + (int.Parse(id.Substring(pos, id.Length - pos)) + 1).ToString("D" + (id.Length - pos));
}


Regards,
Senthil S
 
Share this answer
 
Comments
gani7787 11-Aug-11 8:58am    
Thanks...

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