Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为timespan C生成所有可能的字符串时间格式#_C#_String_Time_Format_Timespan - Fatal编程技术网

C# 为timespan C生成所有可能的字符串时间格式#

C# 为timespan C生成所有可能的字符串时间格式#,c#,string,time,format,timespan,C#,String,Time,Format,Timespan,我正在尝试生成以下TimeSpan字符串格式,由“h:m:s:f”组成,小时、分钟和秒最多为2位,毫秒最多为0到3位 所需格式: hh:m:ss:fff h:mm:s hh:mm:ss h:m:s:ff 等等 我尝试使用递归来实现这一点,但我很难做到这一点,我尝试了以下几点: //Key - minimum digits left //Value starting digits count private static Dictionary<char, KeyValuePair<in

我正在尝试生成以下
TimeSpan
字符串格式,由“h:m:s:f”组成,小时、分钟和秒最多为2位,毫秒最多为0到3位

所需格式:

hh:m:ss:fff

h:mm:s

hh:mm:ss

h:m:s:ff

等等

我尝试使用递归来实现这一点,但我很难做到这一点,我尝试了以下几点:

//Key - minimum digits left
//Value starting digits count
private static Dictionary<char, KeyValuePair<int, int>> replacements =
    new Dictionary<char, KeyValuePair<int, int>>
    {
        ['f'] = new KeyValuePair<int, int>(0, 3),
        ['s'] = new KeyValuePair<int, int>(1, 2),
        ['m'] = new KeyValuePair<int, int>(1, 2),
        ['h'] = new KeyValuePair<int, int>(1, 2)
    };
private static char[] chars = new[] { 'f', 's', 'm', 'h' };
private static string baseTemplate = @"hh\:mm\:ss\:fff";
static IEnumerable<string> GetFormats(string template, int startIndex = 0, int endIndex = 0, List<string> formats = null)
{
    if (formats == null)
    {
        formats = new List<string>{template};
    }
    string copyTemplate = template;
    char currentChar = chars[startIndex];
    int indexToRemove = copyTemplate.IndexOf(currentChar);
    for (int i = 0; i < replacements[currentChar].Value - replacements[currentChar].Key; i++)
    {
        copyTemplate = copyTemplate.Remove(indexToRemove, 1);
        formats.Add(copyTemplate.TrimEnd('\\', '.', ':'));
    }
    if (startIndex == chars.Length - 1 && endIndex == chars.Length - 1)
    {
        return formats;
    }
    if (startIndex == 0)
    {
        return GetFormats(baseTemplate, endIndex + 1, endIndex + 1, formats);
    }
    return GetFormats(copyTemplate, startIndex - 1, endIndex, formats);
}

我如何修复我的递归方法?

不要试图成为一个明智的aleck,OP,但如果这是一个需要我的团队解决的现实问题,我们会这样做:

static public List<string> GetFormats()
{
    return new List<string>
    {
        @"h\:mm\:ss\.fff",
        @"h\:mm\:ss\.ff",
        @"h\:mm\:ss\.f",
        @"h\:mm\:ss",
        @"h\:mm",
        @"hh\:mm\:ss\.fff",
        @"hh\:mm\:ss\.ff",
        @"hh\:mm\:ss\.f",
        @"hh\:mm\:ss",
        @"hh\:mm"
    };
}

在DotNetFiddle上。

不想成为一个聪明的aleck,OP,但如果这是一个需要我的团队解决的现实问题,我们会这样做:

static public List<string> GetFormats()
{
    return new List<string>
    {
        @"h\:mm\:ss\.fff",
        @"h\:mm\:ss\.ff",
        @"h\:mm\:ss\.f",
        @"h\:mm\:ss",
        @"h\:mm",
        @"hh\:mm\:ss\.fff",
        @"hh\:mm\:ss\.ff",
        @"hh\:mm\:ss\.f",
        @"hh\:mm\:ss",
        @"hh\:mm"
    };
}

在DotNetFiddle上。

@mjwills我包含了由“h:m:s:f”组成的约束,其中最多2位表示小时、分钟和秒,0到3位表示毫秒。我这样做是为了将格式数组传递到
TimeSpan.ParseExact
。我当前的版本没有打印所有的组合,大多数都丢失了。
System.Globalization.DateTimeFormatinfo.GetAllDateTimePatterns()
可能是一个更好的开始poiint@Plutonix那很方便,我想一定有更简单的方法,但我还是想练习一下。1个字符
h
m
有什么用?什么时候是
5:1:22
?这一切的意义是什么,凤凰社?如果这是一个严重的业务问题,我只需要在头脑中解决它(实际上很简单),并可能将它们作为字符串常量存储在数组中。那会快得多,因为你只需要做一次。为什么要自动化?为什么要以编程方式“生成”这些?我认为您有一个@mjwills,我已经包含了由“h:m:s:f”组成的约束,其中最多2位表示小时、分钟和秒,0到3位表示毫秒。我这样做是为了将格式数组传递到
TimeSpan.ParseExact
。我当前的版本没有打印所有的组合,大多数都丢失了。
System.Globalization.DateTimeFormatinfo.GetAllDateTimePatterns()
可能是一个更好的开始poiint@Plutonix那很方便,我想一定有更简单的方法,但我还是想练习一下。1个字符
h
m
有什么用?什么时候是
5:1:22
?这一切的意义是什么,凤凰社?如果这是一个严重的业务问题,我只需要在头脑中解决它(实际上很简单),并可能将它们作为字符串常量存储在数组中。那会快得多,因为你只需要做一次。为什么要自动化?为什么要以编程方式“生成”这些?认为你有一个。它们将在应用程序中手工编写,正如我在评论中所说的,我只是认为这是一个有趣的问题需要解决,这就是为什么我在寻找解决方案。我在寻找一种递归方法,但我想这会做到:)它们将在应用程序中手工编写,正如我在评论中所说的,我只是觉得这是一个有趣的问题要解决,这就是为什么我在寻找一个解决方案。我在寻找一个递归方法,但我想这可以:)
static public List<string> GetFormats()
{
    return new List<string>
    {
        @"h\:mm\:ss\.fff",
        @"h\:mm\:ss\.ff",
        @"h\:mm\:ss\.f",
        @"h\:mm\:ss",
        @"h\:mm",
        @"hh\:mm\:ss\.fff",
        @"hh\:mm\:ss\.ff",
        @"hh\:mm\:ss\.f",
        @"hh\:mm\:ss",
        @"hh\:mm"
    };
}
using System;
using System.Linq;
using System.Collections.Generic;

static public class ExtensionMethods
{
    static public IEnumerable<string> AddPossibilities(this IEnumerable<string> input, string symbol, string prefix, int minLength, int maxLength)
    {
        return input
            .SelectMany
                (
                    stringSoFar =>
                        Enumerable.Range
                            (
                                minLength, 
                                maxLength-minLength+1
                            )
                        .Select
                            (
                                length => stringSoFar  +
                                    (
                                        length == 0 ? "" : prefix
                                        + Enumerable.Range(0, length)
                                            .Select(i => symbol)
                                            .Aggregate((c, n) => c + n) 
                                    )
                            )
                );
    }
}

public class Program
{
    public static void Main()
    {
        var results = new List<string> { "" }; //Empty to start
        var list = results
            .AddPossibilities("h", @""   , 1, 2)
            .AddPossibilities("m", @"\:" , 1, 2)
            .AddPossibilities("s", @"\:" , 1, 2)
            .AddPossibilities("f", @"\." , 0, 3);

        var timeSpan = new TimeSpan(0,1,2,3,4);
        foreach (var s in list)
        {
            Console.WriteLine(timeSpan.ToString(s));
        }
    }
}
1:2:3
1:2:3.0
1:2:3.00
1:2:3.004
1:2:03
1:2:03.0
1:2:03.00
1:2:03.004
1:02:3
1:02:3.0
1:02:3.00
1:02:3.004
1:02:03
1:02:03.0
1:02:03.00
1:02:03.004
01:2:3
01:2:3.0
01:2:3.00
01:2:3.004
01:2:03
01:2:03.0
01:2:03.00
01:2:03.004
01:02:3
01:02:3.0
01:02:3.00
01:02:3.004
01:02:03
01:02:03.0
01:02:03.00
01:02:03.004