生成顺序字符串. [英] Generate sequential string.......

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

问题描述

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

推荐答案

在您的Conert方法上不需要执行任何操作.您可以在Main方法上进行操作.创建一个循环,该循环将同时执行n次您的方法,从而增加您的输入参数.您可以参考以下示例.
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.
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());
   }
}


使用string.Format.

参见:
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx [ ^ ].

您可以在此处了解数字对象的格式字符串:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ].

不要写",请使用string.Empty.

—SA
Use string.Format.

See:
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx[^],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx[^].

You can learn format stings for numeric objects here:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Don''t write "", use string.Empty.

—SA


尝试一下:

Try this:

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆