本文介绍了生成顺序字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
        }
    }
}

推荐答案

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());
   }
}



//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()));
       }


这篇关于生成顺序字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:25