字符串操作工具-为开发加速

  平时遇到问题,总习惯于在网上寻找答案,大部分问题都能找到答案。感谢各位大拿的博客及贡献的解决办法。我们不能总是向大家索取,自己也得付出些吧。为此我开始写博客,与大家分享知识,分享快乐。

 

  人与动物的最大区别是什么?人会用工具,而动物不会。在日常的开发中,要面对形形色色的字符串处理。有很多简单而又重复的字符串需要进行处理,如果我们自己写个工具,将会是事半功倍。其实notepad++可以帮助我们。

如:我们将Id,Code,Title,Content,ReportType,ReportDate,RefRecord这几个字符串快速写成属性,只需要写两个正则表达式就Ok.

匹配的正则表达式:(w+),

替换的正则表达式: public string $1 { get; set; }

 

替换后的结果

 

当然属性的类型需要手动去改。

也可以反过来,将属性名从属性中提取出来

 

 

 

下面说下我自己写的一个小工具

 

功能比较简单

【首字母大/写】就不解释了。

【列名转属性】顾名思义就是将数据库中的列名,转成符合C#命名规范的属性名,如:USER_ID,USER_NAME,USER_AGE,转成UserId,UserName,UserAge

 

 

就是将首字母大写去掉下划线,并将下划线后的第一个字母大写,其它字母小写。

 

【正则提取】是写个正则表达式,提取出索引部分,索引从0开始,0表示整个表达式

 

 

接下来说下代码实现:

四个按钮对应了四个自己写的stirng的扩展方法,对扩展方法不熟的朋友,可先看这篇博客学习下http://www.cnblogs.com/suger/archive/2012/05/13/2498248.html, 这里不再详说。

这里的我单独写了个类库,别的地方要用,引用dll和命名空间即可。

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Text.RegularExpressions;
  7 using System.Threading;
  8 using System.Threading.Tasks;
  9 
 10 namespace System
 11 {
 12     public static class StringExt
 13     {
 14         /// <summary>
 15         /// 正则表达提取
 16         /// </summary>
 17         /// <param name="input">输入字符串</param>
 18         /// <param name="expression">正则表达式</param>
 19         /// <param name="index">索引</param>
 20         /// <returns>返回匹配后的结果</returns>
 21         public static string RegexExtraction(this string input, string expression, int index)
 22         {
 23             if (string.IsNullOrEmpty(input))
 24             {
 25                 return input;
 26             }
 27             var mc = Regex.Matches(input, expression);
 28             var sb = new StringBuilder();
 29             foreach (Match match in mc)
 30             {
 31                 sb.Append(match.Groups[index] + "
");
 32             }
 33             return sb.ToString();
 34         }
 35 
 36         /// <summary>
 37         /// 正则表达提取
 38         /// </summary>
 39         /// <param name="input">输入字符串</param>
 40         /// <param name="expression">正则表达式</param>
 41         /// <returns>返回匹配后的结果</returns>
 42         public static string RegexExtraction(this string input, string expression)
 43         {
 44             return RegexExtraction(input, expression, 0);
 45         }
 46 
 47         /// <summary>
 48         /// 首字母大写
 49         /// </summary>
 50         /// <param name="text">要转换的字符串</param>
 51         /// <returns>返回转换结果</returns>
 52         public static string ToTitleCase(this string text)
 53         {
 54             if (string.IsNullOrEmpty(text))
 55             {
 56                 return text;
 57             }
 58             return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(text);
 59         }
 60 
 61         /// <summary>
 62         /// 首字母小写
 63         /// </summary>
 64         /// <param name="text">要转换的字符串</param>
 65         /// <returns>返回转换结果</returns>
 66         public static string FirstLetterLower(this string text)
 67         {
 68             if (string.IsNullOrEmpty(text))
 69             {
 70                 return text;
 71             }
 72             var firstLetter = text[0];
 73             var result = text;
 74             if (firstLetter >= 65 && firstLetter <= 90)
 75             {
 76                 firstLetter = Convert.ToChar(firstLetter + 32);
 77                 result = firstLetter + text.Substring(1, text.Length - 1);
 78             }
 79             return result;
 80         }
 81         
 82         /// <summary>
 83         /// 列名转属性名
 84         /// </summary>
 85         /// <param name="columnName">列名</param>
 86         /// <returns>返回属性名</returns>
 87         public static string ToPropertyName(this string columnName)
 88         {
 89             if (string.IsNullOrEmpty(columnName))
 90             {
 91                 return columnName;
 92             }
 93             columnName = columnName.ToLower();
 94             var words = columnName.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
 95             var sb = new StringBuilder();
 96             foreach (var word in words)
 97             {
 98                 sb.Append(ToTitleCase(word));
 99             }
100             return sb.ToString();
101         }
102     }
103 }

 

以上纯属个人见解,如有不足与错误之处敬请批评指正。

原文地址:https://www.cnblogs.com/xiaoafei1991/p/4088984.html