Click here to Skip to main content
15,914,446 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check_conver
{
    class Program
    {
        //inp should be like FILE0001
        public void Conert(string inp)
        {
            Console.WriteLine("String is :{0}", inp);
            string sr = inp.Replace("FILE", "");
            int countchar = Convert.ToInt32(sr.Length);
            int num = Convert.ToInt32(sr);
            int numlngth = Convert.ToInt32(Convert.ToString(num).Length);
            Console.WriteLine("After removing char,the string is : {0}", sr);
            Console.WriteLine("String Length After Replacing Char : {0}", countchar);
            Console.WriteLine("Now no for calculation is :{0}", num);
            Console.WriteLine("Zero's Before no Should Be :{0}", countchar - numlngth);


            //.. now i want .. next string should be like. FILE0002.. then FILE0003 and so on..

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Conert("FILE0001");
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jul-11 2:29am    
What is non-sequential string? :-)
What is your problem?
--SA
[no name] 6-Jul-11 2:36am    
actly dear.. Its a primary key in sql server Like FILE0001,so i want to increase it like FILE0002,FILE0003.... like this... being confuse.. coz its a string and if i m converting it to int for calculation after replacing FILE it showing 1 not 0001....
plz.. help me out here..

There's nothing that needs to be done on your Conert method. You do it on your Main method. Create a loop that will execute your method n times at the same time, incrementing your input parameter. You may refer to the following example.
C#
static void Main(string[] args)
{
   Program p = new Program();
   for(int i=1; i < 10; i++) //suppose you want FILE0001 to FILE0009
   {
      p.Conert("FILE000" + i.ToString());
   }
}
 
Share this answer
 
Comments
[no name] 6-Jul-11 2:38am    
thanx.. dear.. but 1 to 9 is not fix...it can be FILE0999 then..
walterhevedeich 6-Jul-11 3:00am    
I see. But the idea is there. It should be pretty easy to modify the code to suit your scenario.
[no name] 6-Jul-11 4:54am    
Thanx Dear..
 
Share this answer
 
Try this:

C#
//inp should be like FILE0001
       public void Conert(string inp)
       {
           string sr = inp.Replace("FILE", "");
           int num = Convert.ToInt32(sr);
           int leadingzeros = (Convert.ToInt32(sr.Length) - Convert.ToInt32(Convert.ToString(num).Length)) ;
           int newlen = num.ToString("D").Length + leadingzeros;
           num += 1;
           MessageBox.Show("FILE" + num.ToString("D" + newlen.ToString()));
       }
 
Share this answer
 
Comments
Om Prakash Pant 6-Jul-11 3:07am    
try this, this will work irrespective of any number of zeros
[no name] 6-Jul-11 4:54am    
Its Working.. Thank you very much...

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