如何通过一行进行解析

本文关键字:一行 何通过 | 更新日期: 2023-09-27 18:21:44

我正在寻找一个关于如何在一行内获得所有int解析的解决方案。在我开始我的程序的那一刻,我必须输入日期、月份和年份。这一切都是逐行完成的。我想要一个解决方案或方法,它在一行中以"dd/MM/yyyy"的格式完成我的所有解析。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace date
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("please enter date as dd/MM/yyyy");
            int day;
            int month;
            int year;

            day = int.Parse(Console.ReadLine());
            month = int.Parse(Console.ReadLine());
            year = int.Parse(Console.ReadLine());
            Date i = new Date(day, month, year);

            Console.WriteLine("{0}/{1}/{2}", i.day, i.month, i.year);
            Console.ReadLine();
        }
        class Date
        {
            public int month; // 1-12
            public int day; // 1-31 depending on month
            int value = 1;
            public int year
            {
                get;
                private set;
            }

            public Date(int day, int month, int year)
            {
                this.day = day;
                this.month = month;
                this.year = year;
            }
            public int GetYear()
            {
                return year;
            }
            public void SetYear()
            {
                if (value > 1900 && value <= 2020)
                    year = value;
                else
                    throw new ArgumentOutOfRangeException("year", value, "out of bounds");
            }
            private int Month
            {
                get { return month; }
                set
                {
                    if (value > 0 && value <= 12)
                        month = value;
                    else
                        throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");
                }
            }
            public int GetDay()
            {
                return day;
            }
            public void SetDay()
            {
                int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                if (value > 0 && value <= days[month])
                    day = value;
                else if (month == 2 && value == 29 &&
                    year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                    day = value;
                else
                    throw new ArgumentOutOfRangeException("days", value, "day is out of range");
            }
        }
    }
}

如何通过一行进行解析

您可以使用DateTime.Parse/TryParseDateTime.ParseExact/TryParseExact:

string line = Console.ReadLine();
DateTime dt;
bool validDate = DateTime.TryParseExact(line,"dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo,DateTimeStyles.None, out dt);
if(validDate)
    Console.WriteLine(dt.ToLongDateString()); // now correctly initialized 

DateTime.Parse/DateTime.TryParse也使用这种格式:

validDate = DateTime.TryParse(line, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt);

我使用DateTimeFormatInfo.InvariantInfo来防止使用本地日期分隔符而不是/(以防不同)。

一旦它被解析为DateTime,创建Date实例就很简单了:

Date d = new Date(dt.Day, dt.Month, dt.Year);

如果您不想使用DateTime。(试试)Parse,你可以在Date类中添加一个带有Regex的解析方法:

public static Date ParseFromString(string s)
  {
     //string s = "24/12/2015";
     Regex r = new Regex(@"('d+)['/]('d+)['/]('d+)");
     Match m = r.Match(s);
     if (m.Success)
     {
        return new Date(m.Groups[1], m.Groups[2], m.Groups[3]);
     }
     else
     {
         // throw exception
     }
  }

无法将dd/MM/yyyy格式的字符串解析为int,因为它不是有效字符串。

您需要首先将其解析为DateTime,然后可以使用它的属性将其日期、月份和年份作为数字。

例如;

Console.WriteLine("please enter date as dd/MM/yyyy");
DateTime dt = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy",
                                  CultureInfo.InvariantCulture);
int day = dt.Day;
int month = dt.Month;
int year = dt.Year;

如果您不想在输入不是dd/MM/yyyy格式的情况下抛出异常,也可以使用TryParseExact

我不想使用预设的DateTime类,但我正在使用自己的"日期"类

如果是这样的话,在您解析它之后,您可以基于此dt值使用Date(int day, int month, int year)构造函数创建Date类实例为;

Date myDt = new Date(dt.Day, dt.Month, dt.Year);