迁移到控制台应用程序和用户输入

时间:2015-09-05 18:24:33

标签: c# .net string split console-application

我正在使用带有c#的.net应用程序,但我想将其迁移到c#console应用程序

类:

static class StringExtensions 
{
     public static IEnumerable<String> SplitInParts(this String s, Int32 partLength) 
     {
         if (s == null)
            throw new ArgumentNullException("s");
         if (partLength <= 0)
            throw new ArgumentException("Part length has to be positive.", "partLength");

         for (var i = 0; i < s.Length; i += partLength)
            yield return s.Substring(i, Math.Min(partLength, s.Length - i));
      }

}

主叫:

var parts = "32427237".SplitInParts(3);
Console.WriteLine(String.Join(" ", parts));

//输出为324 272 37

所以我想用户输入值,例如123456789,分割输出是123 456 789,我怎么能在控制台应用上做呢?

------------------------------ UPDATE ---------------- --------------------------

完整代码:

namespace prw
{
    class Program
    {
        public static IEnumerable<String> SplitInParts(this String s, Int32 partLength)
        {
            if (s == null)
               throw new ArgumentNullException("s");
            if (partLength <= 0)
               throw new ArgumentException("Part length has to be positive.", "partLength");

            for (var i = 0; i < s.Length; i += partLength)
               yield return s.Substring(i, Math.Min(partLength, s.Length - i));
        }




         private static void Main(string[] args)
         {
            var input= Console.ReadLine();
            var parts = input.SplitInParts(3);
            Console.WriteLine(String.Join(" ", parts));   
         }
    }
}

1 个答案:

答案 0 :(得分:1)

class Program
{
    private static void Main(string[] args)
    {
        var input= Console.ReadLine();
        var parts = input.SplitInParts(3);
        Console.WriteLine(String.Join(" ", parts));   
   }
}
 public static class Util
{
    public static IEnumerable<String> SplitInParts(this String s, Int32 partLength)
    {
        if (s == null)
            throw new ArgumentNullException("s");
        if (partLength <= 0)
            throw new ArgumentException("Part length has to be positive.", "partLength");

        for (var i = 0; i < s.Length; i += partLength)
            yield return s.Substring(i, Math.Min(partLength, s.Length - i));
    }
}