zoukankan      html  css  js  c++  java
  • Use a String.Format format and transform its output to its inputs?

    ParseExact for Strings?

     

    The DateTime.ParseExact() method has come in handy on a number of occasions. I really wish there was something similar for strings as sometimes you need to grab specific components of a string. You can use regex directly but that seems a little cumbersome. So here is an extension method that works like the DateTime.ParseExact() but returns an array of strings.

    string value = "2/17/2009 10:57:42 AM...Executing file 26 of 81 files";
    string[] parts = value.ParseExact("{0}...Executing file {1} of {2} files");
    foreach (string part in parts)
        Console.WriteLine(part);
    Console.ReadKey();
    image 

    Implementation:

    public static class StringExtensions
    {
        public static string[] ParseExact(
            this string data, 
            string format)
        {
            return ParseExact(data, format, false);
        }
    
        public static string[] ParseExact(
            this string data, 
            string format, 
            bool ignoreCase)
        {
            string[] values;
    
            if (TryParseExact(data, format, out values, ignoreCase))
                return values;
            else
                throw new ArgumentException("Format not compatible with value.");
        }
    
        public static bool TryExtract(
            this string data, 
            string format, 
            out string[] values)
        {
            return TryParseExact(data, format, out values, false);
        }
    
        public static bool TryParseExact(
            this string data, 
            string format, 
            out string[] values, 
            bool ignoreCase)
        {
            int tokenCount = 0;
            format = Regex.Escape(format).Replace("\\{", "{");
    
            for (tokenCount = 0; ; tokenCount++)
            {
                string token = string.Format("{{{0}}}", tokenCount);
                if (!format.Contains(token)) break;
                format = format.Replace(token,
                    string.Format("(?'group{0}'.*)", tokenCount));
            }
    
            RegexOptions options = 
                ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
    
            Match match = new Regex(format, options).Match(data);
    
            if (tokenCount != (match.Groups.Count - 1))
            {
                values = new string[] { };
                return false;
            }
            else
            {
                values = new string[tokenCount];
                for (int index = 0; index < tokenCount; index++)
                    values[index] = 
                        match.Groups[string.Format("group{0}", index)].Value;
                return true;
            }
        }
    }
    
    原帖:
    http://blog.mikeobrien.net/2009/02/parseexact-for-strings.html
  • 相关阅读:
    Oracle数据库面试题【转载】
    年龄计算周岁
    丈夫的权力与妻子的职业水平
    JDK 8 and JRE 8 Supported Locales
    一笔画 奇点 偶点
    流水行船问题
    PL/SQL LOOP SAMPLE
    OpenCV——识别各省份地图轮廓
    OpenCV——轮廓面积及长度计算
    树莓派3安装opencv2程序无法运行
  • 原文地址:https://www.cnblogs.com/leon032/p/2453595.html
Copyright © 2011-2022 走看看