微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将选定的单词转换为特定的大小写

如何解决将选定的单词转换为特定的大小写

我有一个单词列表,我需要将其格式化为每个单词的特定大小写,但是我当前使用的方法效率极低。

单词列表和每个单词所需的大小写:

    private static readonly string[] ProperCaseHeaders = { "Accept","Accept-Encoding","Accept-Language","Alert-Info","Allow","Authentication-Info","Authorization","Call-ID","Call-Info","Contact","Content-disposition","content-encoding","Content-Language","Content-Length","Content-Type","CSeq","Date","Error-Info","Expires","From","In-Reply-To","Max-Forwards","Min-Expires","MIME-Version","Organization","Priority","Proxy-Authenticate","Proxy-Authorization","Proxy-Require","Record-Route","Reply-To","Require","Retry-After","Route","Server","Subject","Supported","Timestamp","To","Unsupported","User-Agent","Via","Warning","WWW-Authenticate" };

我使用的方法

    public static string ToProperCaseHeader(this string header)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader,StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

以及性能分析结果:

enter image description here

如您所见,大部分时间都花在了使用 InvariantCultureIgnoreCase 的比较上。

标题可能出现在各种外壳中,在所有情况下,我都希望它们被标准化。例如:

CALL-ID、call-id、Call-Id、CALL-id 都应该标准化为 Call-ID。 CONTENT-TYPE、content-type、Content-type、CONTENT-type都应该归一化为Content-Type。

这不一定是简单的适当大小写转换。

有人有什么想法吗?

解决方法

以下是您可以用代码替换以提高性能的代码。

class Program
{
    private static readonly string[] ProperCaseHeaders = { "Accept","Accept-Encoding","Accept-Language","Alert-Info","Allow","Authentication-Info","Authorization","Call-ID","Call-Info","Contact","Content-Disposition","Content-Encoding","Content-Language","Content-Length","Content-Type","CSeq","Date","Error-Info","Expires","From","In-Reply-To","Max-Forwards","Min-Expires","MIME-Version","Organization","Priority","Proxy-Authenticate","Proxy-Authorization","Proxy-Require","Record-Route","Reply-To","Require","Retry-After","Route","Server","Subject","Supported","Timestamp","To","Unsupported","User-Agent","Via","Warning","WWW-Authenticate" };

    static void Main(string[] args)
    {
        Dictionary<string,string> headers = new Dictionary<string,string>();

        foreach (var item in ProperCaseHeaders)
            headers.Add(item.ToLower(),item);

        Stopwatch stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].ToProperCaseHeader(ProperCaseHeaders)}");
        Console.WriteLine($"Using Old Method End {stopwatch.ElapsedMilliseconds}");

        Console.WriteLine();

        stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].GetProperCaseHeader(headers)}");
        Console.WriteLine($"Using New Method End {stopwatch.ElapsedMilliseconds}");

        Console.ReadLine();
    }

}

public static class Extension
{
    public static string ToProperCaseHeader(this string header,string[] ProperCaseHeaders)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader,StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

    public static string GetProperCaseHeader(this string header,Dictionary<string,string> headers)
    {
        var response = string.Empty;
        headers.TryGetValue(header.ToLower(),out response);
        return response;
    }
}

结果如下。

Specific header for aleRt-Info Alert-Info
Using Old Method End 48

Specific header for aleRt-Info Alert-Info
Using New Method End 1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。