C#字符串扩展方法(分享·原创)

View Code
  1  #region String 扩展
  2         /// 去掉字符串的最后一个逗号
  3         /// <summary>
  4         /// 去掉字符串的最后一个逗号
  5         /// </summary>
  6         /// <param name="s">调用该方法的当前实例</param>
  7         /// <returns>如果该字符串是以“,”结尾,那么将去掉该“,”,然后返回剩余的字符串;否则返回原字符串。</returns>
  8         /// by:TYH
  9         public static string RemoveLastComma(this string s)
 10         {
 11             if (s == null)
 12                 return null;
 13 
 14             //return s.EndsWith(",") ? s.Remove(s.Length - 1, 1) : s;
 15             return s.TrimEnd(',');
 16 
 17         }
 18         /// 将逗号隔开的字符串转换成对应的list int 
 19         /// <summary>
 20         /// 将逗号隔开的字符串转换成对应的list int 
 21         /// </summary>
 22         /// <param name="str"></param>
 23         /// <returns></returns>
 24         /// by:TYH
 25         public static List<int> GetIntList(this string str)
 26         {
 27             List<int> int_list = new List<int>();
 28             if (str != null)
 29             {
 30                 string[] tmp = str.Split(',');
 31                 List<string> tmp2 = tmp.ToList();
 32                 Func<string, int> toInt = s => { int i; int.TryParse(s, out i); return i; };
 33                 int_list.AddRange(tmp2.ConvertAll(new Converter<string, int>(toInt)));
 34             }
 35             return int_list;
 36         }
 37         /// 将逗号隔开的字符串转换成对应的list string 
 38         /// <summary>
 39         /// 将逗号隔开的字符串转换成对应的list string 
 40         /// </summary>
 41         /// <param name="str"></param>
 42         /// <returns></returns>
 43         /// by:TYH
 44         public static List<string> GetStringList(this string str)
 45         {
 46             List<string> string_list = new List<string>();
 47             if (str != null)
 48             {
 49                 string[] tmp = str.Split(',');
 50                 string_list.AddRange(tmp.ToList());
 51             }
 52             return string_list;
 53         }
 54         /// 在字符串最后面加上半角“,”
 55         /// <summary>
 56         /// 在字符串最后面加上半角“,”
 57         /// </summary>
 58         /// <param name="s"></param>
 59         /// <returns></returns>
 60         /// by:TYH
 61         public static string AppendLastComma(this string s)
 62         {
 63             if (s != null)
 64                 return !s.EndsWith(",") ? s + "," : s;
 65             else return s;
 66         }
 67         /// 假如字符串超过指定的长度,那么留取指定的长度并加上省略号,然后返回这样处理后的字符串
 68         /// <summary>
 69         /// 假如字符串超过指定的长度,那么留取指定的长度并加上省略号,然后返回这样处理后的字符串
 70         /// 否则返回原字符串
 71         /// </summary>
 72         /// <param name="s"></param>
 73         /// <param name="i">指定留取的长度</param>
 74         /// <returns></returns>
 75         /// by:TYH
 76         public static string SubStringThenAppendEllipsis(this string s, int i)
 77         {
 78             if (s.Length > i)
 79                 return s.Substring(0, i) + "";
 80             else
 81                 return s;
 82         }
 83         /// 如果字符串实例为null或者空字符串,则返回“(无)”,括号为半角;否则返回原字符串
 84         /// <summary>
 85         /// 如果字符串实例为null或者空字符串,则返回“(无)”,括号为半角;否则返回原字符串
 86         /// </summary>
 87         /// <param name="s"></param>
 88         /// <returns></returns>
 89         /// by:TYH
 90         public static string AppendWUIfNothing(this string s)
 91         {
 92             if (s == null || s == "")
 93                 return "(无)";
 94             else return s;
 95         }
 96         /// 文件名不能含这9个字符 / \ : " * ?  大于  小于  |
 97         /// <summary>
 98         /// 移除文件名的不合法字符 
 99         /// 文件名不能含这9个字符 / \ : " * ?  大于  小于  |
100         /// </summary>
101         /// <param name="s"></param>
102         /// <returns></returns>
103         /// by:TYH
104         public static string ChechValidOfFileName(this String s)
105         {
106             if (s == null)
107                 return null;
108 
109             if (s.Contains("/"))
110                 s = s.Replace("/", "_");
111             if (s.Contains("\\"))
112                 s = s.Replace("\\", "_");
113 
114             if (s.Contains(":"))
115                 s = s.Replace(":", "_");
116             if (s.Contains("\""))
117                 s = s.Replace("\"", "_");
118 
119             if (s.Contains("*"))
120                 s = s.Replace("*", "_");
121             if (s.Contains("?"))
122                 s = s.Replace("?", "_");
123 
124             if (s.Contains("<"))
125                 s = s.Replace("<", "_");
126             if (s.Contains(">"))
127                 s = s.Replace(">", "_");
128 
129             if (s.Contains("|"))
130                 s = s.Replace("|", "_");
131             return s;
132         }
133         /// 路径名不能含这4个字符 "   大于  小于  |
134         /// 可以含这5个字符 : / \ : * ?
135         /// <summary>
136         /// 移除路径名的不合法字符 
137         /// 路径名不能含这4个字符 "   大于  小于  |
138         /// </summary>
139         /// <param name="s"></param>
140         /// <returns></returns>
141         /// by:TYH
142         public static string ChechValidOfPathName(this String s)
143         {
144             if (s == null)
145                 return null;
146 
147             if (s.Contains("\""))
148                 s = s.Replace("\"", "_");
149 
150 
151             if (s.Contains("<"))
152                 s = s.Replace("<", "_");
153 
154             if (s.Contains(">"))
155                 s = s.Replace(">", "_");
156 
157             if (s.Contains("|"))
158                 s = s.Replace("|", "_");
159             return s;
160         }
161         /// 给字符串后附件无特殊字符的GIUD,用于文件名,以免文件已经存在
162         /// <summary>
163         /// 给字符串后附件无特殊字符的GIUD,用于文件名,以免文件已经存在
164         /// </summary>
165         /// <param name="s"></param>
166         /// <returns></returns>
167         public static string AppendGUIDWithoutSpecialChar(this String s)
168         {
169             return s + Guid.NewGuid().ToString("N");
170         }
171         /// 将Ascii码十进制所代表的字符替换成指定的字符
172         /// <summary>
173         /// 将ASCII码十进制所代表的字符替换成指定的字符
174         /// </summary>
175         /// <param name="oldStr"></param>
176         /// <param name="oldChar"></param>
177         /// <param name="newChar"></param>
178         /// <returns></returns>
179         public static string Replace(this string oldStr, byte oldChar, char newChar)
180         {
181             char ch = (char)oldChar;
182             return oldStr.Replace(ch, newChar);
183         }
184         /// 将Ascii码十进制所代表的字符替换成指定的字符
185         /// <summary>
186         /// 将ASCII码十进制所代表的字符替换成指定的字符
187         /// </summary>
188         /// <param name="oldStr"></param>
189         /// <param name="oldChar"></param>
190         /// <param name="newChar"></param>
191         /// <returns></returns>
192         public static string Replace(this string oldStr, byte oldChar, String newStr)
193         {
194             char ch = (char)oldChar;
195             return oldStr.Replace(ch.ToString(), newStr);
196         }
197         /// 获取纯文件名:
198         /// <summary>
199         /// 获取纯文件名:
200         /// 会检查路径的合法性
201         /// 会检查重复的扩展名
202         /// </summary>
203         /// <param name="filename"></param>
204         /// <returns></returns>
205         public static string GetFilename(this string filename)
206         {
207             if (filename == null)
208                 return null;
209 
210             filename = System.IO.Path.GetFileName(filename.ChechValidOfPathName());
211 
212             //只考虑有两个扩展名的情况
213             string FileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
214 
215             string FileNameWithoutExtension2 = Path.GetFileNameWithoutExtension(FileNameWithoutExtension);
216 
217 
218             if (FileNameWithoutExtension == FileNameWithoutExtension2)
219                 return filename;
220             else return FileNameWithoutExtension;
221 
222         }
223         /// 判断字段是否由  null 、 空  或 仅空白字符串组成
224         /// <summary>
225         /// 判断字段是否由  null 、 空  或 仅空白字符串组成
226         /// </summary>
227         /// <param name="str"></param>
228         /// <returns></returns>
229         public static bool IsNullOrWhiteSpace(this string str)
230         {
231             return string.IsNullOrWhiteSpace(str);
232         }
233         /// 判断字符串是否 由  Null  或 空 字符串 组成
234         /// <summary>
235         /// 判断字符串是否 由  Null  或 空 字符串 组成
236         /// </summary>
237         /// <param name="str"></param>
238         /// <returns></returns>
239         public static bool IsNullOrEmpty(this string str)
240         {
241             return string.IsNullOrEmpty(str);
242         }
243         #endregion

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值