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

本文关键字:用户 输入 应用程序 控制台 迁移 | 更新日期: 2023-09-27 18:11:33

我正在使用。net应用程序与c#,但我想把它迁移到c#控制台应用程序

类:

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,分割输出是123456789,我怎么能在控制台应用程序上做到这一点?

------------------------------ 更新 ------------------------------------------

完整代码:

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

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

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