常用——扩展方法
1 /// <summary>
2 ///ExpandMethod 扩展方法
3 /// </summary>
4 public static class ExpandMethod
5 {
6 /// <summary>
7 /// 字符串是否为null或者""
8 /// true:是null或者"";false:则反之
9 /// </summary>
10 /// <param name="s"></param>
11 /// <returns></returns>
12 public static bool IsNullOrEmpty(this string s)
13 {
14 return String.IsNullOrEmpty(s);
15 }
16
17 /// <summary>
18 /// 如果为空或"" 返回 null,反之原样返回
19 /// </summary>
20 /// <param name="s"></param>
21 /// <returns></returns>
22 public static string GetNoNullOrEmpty(this string s)
23 {
24 if (!String.IsNullOrEmpty(s))
25 {
26 return s;
27 }
28 return null;
29 }
30
31 /// <summary>
32 /// 不为空时返回日期部分字符串,为空nullstring替换返回
33 /// </summary>
34 /// <param name="date"></param>
35 /// <param name="nullstring"></param>
36 /// <returns></returns>
37 public static string DateTimeToString(this DateTime? date, string nullstring)
38 {
39 if (date.HasValue)
40 {
41 return date.Value.ToShortDateString();
42 }
43 else
44 {
45 return nullstring;
46 }
47 }
48
49 /// <summary>
50 /// 格式化DateTime输出格式化后字符串
51 /// </summary>
52 /// <param name="date"></param>
53 /// <param name="formatstr">如 yyyy'/'mm'/'dd</param>
54 /// <returns></returns>
55 public static string DateTimeFormatWith(this DateTime date, string formatstr)
56 {
57 return date.ToString(formatstr);
58 }
59
60 /// <summary>
61 /// DateTime? 格式化输出字符串
62 /// </summary>
63 /// <param name="date"></param>
64 /// <param name="formatstr">如 yyyy'/'mm'/'dd</param>
65 /// <param name="nullstr"></param>
66 /// <returns></returns>
67 public static string DateTimeFormatWith(this DateTime? date, string formatstr, string nullstr)
68 {
69 if (date.HasValue)
70 {
71 return date.Value.DateTimeFormatWith(formatstr);
72 }
73 else
74 {
75 return date.DateTimeToString(nullstr);
76 }
77 }
78
79 /// <summary>
80 /// 不为空时返回日期部分字符串,否则返回null
81 /// </summary>
82 /// <param name="date"></param>
83 /// <returns></returns>
84 public static string DateTimeToString(this DateTime? date)
85 {
86 if (date.HasValue)
87 {
88 return date.Value.ToShortDateString();
89 }
90 else
91 {
92 return null;
93 }
94 }
95
96 /// <summary>
97 /// 判断字符串是否符合正则表达式
98 /// </summary>
99 /// <param name="s"></param>
100 /// <param name="pattern">正则表达式</param>
101 /// <returns></returns>
102 public static bool IsMatch(this string s, string pattern)
103 {
104 if (s == null) return false;
105 else return Regex.IsMatch(s, pattern);
106 }
107
108 /// <summary>
109 /// 获取符合正则表达式的值,如果对象为null 返回""
110 /// </summary>
111 /// <param name="s"></param>
112 /// <param name="pattern"></param>
113 /// <returns></returns>
114 public static string Match(this string s, string pattern)
115 {
116 if (s == null) return "";
117 return Regex.Match(s, pattern).Value;
118 }
119
120 /// <summary>
121 /// 判断是否为Int,是:true;否:false
122 /// </summary>
123 /// <param name="s"></param>
124 /// <returns></returns>
125 public static bool IsInt(this string s)
126 {
127 int i;
128 return int.TryParse(s, out i);
129 }
130
131 /// <summary>
132 /// 将字符串转为Int
133 /// </summary>
134 /// <param name="s"></param>
135 /// <returns></returns>
136 public static int ToInt(this string s)
137 {
138 try
139 {
140 return int.Parse(s);
141 }
142 catch (Exception)
143 {
144 return 0;
145 }
146 }
147
148 public static int? ToIntOrDefault(this string s)
149 {
150 if (!s.IsNullOrEmpty())
151 {
152 if (s.IsInt())
153 {
154 return s.ToInt();
155 }
156 else
157 {
158 return null;
159 }
160 }
161 else
162 {
163 return null;
164 }
165 }
166
167 /// <summary>
168 /// int? 类型不为null 或 0,转换为string
169 /// </summary>
170 /// <param name="ent"></param>
171 /// <returns></returns>
172 public static string IsNullInt(this int? ent)
173 {
174 if (ent.HasValue&&ent.Value>0)
175 {
176 return ent.Value.ToString();
177 }
178 else
179 {
180 return null;
181 }
182 }
183
184 /// <summary>
185 /// 判断字符串的值是否为double,是:true;否:false
186 /// </summary>
187 /// <param name="s"></param>
188 /// <returns></returns>
189 public static bool IsDouble(this string s)
190 {
191 double d;
192 return double.TryParse(s, out d);
193 }
194
195 /// <summary>
196 /// 将字符串转换为double
197 /// </summary>
198 /// <param name="s"></param>
199 /// <returns></returns>
200 public static double ToDouble(this string s)
201 {
202 return double.Parse(s);
203 }
204
205 public static double? ToDoubleOrDefault(this string s)
206 {
207 if (s.IsDouble()&& !s.IsNullOrEmpty())
208 {
209 return double.Parse(s);
210 }
211 else
212 {
213 return null;
214 }
215 }
216
217 /// <summary>
218 /// 判断是否为空或0,是返回null;否返回string
219 /// </summary>
220 /// <param name="d"></param>
221 /// <returns></returns>
222 public static string IsNullDecimal(this decimal? d)
223 {
224 if (d.HasValue&&d.Value>0)
225 {
226 return d.Value.ToString();
227 }
228 else
229 {
230 return null;
231 }
232 }
233
234 /// <summary>
235 /// 转换成decimal 不成功返回 null
236 /// </summary>
237 /// <param name="d"></param>
238 /// <returns></returns>
239 public static decimal? ToDecimal(this string d)
240 {
241 if (!d.IsNullOrEmpty() && d.IsDecimal())
242 {
243 return decimal.Parse(d);
244 }
245 else
246 {
247 return null;
248 }
249 }
250
251 /// <summary>
252 /// 是否为decimal类型
253 /// </summary>
254 /// <param name="s"></param>
255 /// <returns></returns>
256 public static bool IsDecimal(this string s)
257 {
258 decimal d;
259 return decimal.TryParse(s,out d);
260 }
261
262
276
277 }
2 ///ExpandMethod 扩展方法
3 /// </summary>
4 public static class ExpandMethod
5 {
6 /// <summary>
7 /// 字符串是否为null或者""
8 /// true:是null或者"";false:则反之
9 /// </summary>
10 /// <param name="s"></param>
11 /// <returns></returns>
12 public static bool IsNullOrEmpty(this string s)
13 {
14 return String.IsNullOrEmpty(s);
15 }
16
17 /// <summary>
18 /// 如果为空或"" 返回 null,反之原样返回
19 /// </summary>
20 /// <param name="s"></param>
21 /// <returns></returns>
22 public static string GetNoNullOrEmpty(this string s)
23 {
24 if (!String.IsNullOrEmpty(s))
25 {
26 return s;
27 }
28 return null;
29 }
30
31 /// <summary>
32 /// 不为空时返回日期部分字符串,为空nullstring替换返回
33 /// </summary>
34 /// <param name="date"></param>
35 /// <param name="nullstring"></param>
36 /// <returns></returns>
37 public static string DateTimeToString(this DateTime? date, string nullstring)
38 {
39 if (date.HasValue)
40 {
41 return date.Value.ToShortDateString();
42 }
43 else
44 {
45 return nullstring;
46 }
47 }
48
49 /// <summary>
50 /// 格式化DateTime输出格式化后字符串
51 /// </summary>
52 /// <param name="date"></param>
53 /// <param name="formatstr">如 yyyy'/'mm'/'dd</param>
54 /// <returns></returns>
55 public static string DateTimeFormatWith(this DateTime date, string formatstr)
56 {
57 return date.ToString(formatstr);
58 }
59
60 /// <summary>
61 /// DateTime? 格式化输出字符串
62 /// </summary>
63 /// <param name="date"></param>
64 /// <param name="formatstr">如 yyyy'/'mm'/'dd</param>
65 /// <param name="nullstr"></param>
66 /// <returns></returns>
67 public static string DateTimeFormatWith(this DateTime? date, string formatstr, string nullstr)
68 {
69 if (date.HasValue)
70 {
71 return date.Value.DateTimeFormatWith(formatstr);
72 }
73 else
74 {
75 return date.DateTimeToString(nullstr);
76 }
77 }
78
79 /// <summary>
80 /// 不为空时返回日期部分字符串,否则返回null
81 /// </summary>
82 /// <param name="date"></param>
83 /// <returns></returns>
84 public static string DateTimeToString(this DateTime? date)
85 {
86 if (date.HasValue)
87 {
88 return date.Value.ToShortDateString();
89 }
90 else
91 {
92 return null;
93 }
94 }
95
96 /// <summary>
97 /// 判断字符串是否符合正则表达式
98 /// </summary>
99 /// <param name="s"></param>
100 /// <param name="pattern">正则表达式</param>
101 /// <returns></returns>
102 public static bool IsMatch(this string s, string pattern)
103 {
104 if (s == null) return false;
105 else return Regex.IsMatch(s, pattern);
106 }
107
108 /// <summary>
109 /// 获取符合正则表达式的值,如果对象为null 返回""
110 /// </summary>
111 /// <param name="s"></param>
112 /// <param name="pattern"></param>
113 /// <returns></returns>
114 public static string Match(this string s, string pattern)
115 {
116 if (s == null) return "";
117 return Regex.Match(s, pattern).Value;
118 }
119
120 /// <summary>
121 /// 判断是否为Int,是:true;否:false
122 /// </summary>
123 /// <param name="s"></param>
124 /// <returns></returns>
125 public static bool IsInt(this string s)
126 {
127 int i;
128 return int.TryParse(s, out i);
129 }
130
131 /// <summary>
132 /// 将字符串转为Int
133 /// </summary>
134 /// <param name="s"></param>
135 /// <returns></returns>
136 public static int ToInt(this string s)
137 {
138 try
139 {
140 return int.Parse(s);
141 }
142 catch (Exception)
143 {
144 return 0;
145 }
146 }
147
148 public static int? ToIntOrDefault(this string s)
149 {
150 if (!s.IsNullOrEmpty())
151 {
152 if (s.IsInt())
153 {
154 return s.ToInt();
155 }
156 else
157 {
158 return null;
159 }
160 }
161 else
162 {
163 return null;
164 }
165 }
166
167 /// <summary>
168 /// int? 类型不为null 或 0,转换为string
169 /// </summary>
170 /// <param name="ent"></param>
171 /// <returns></returns>
172 public static string IsNullInt(this int? ent)
173 {
174 if (ent.HasValue&&ent.Value>0)
175 {
176 return ent.Value.ToString();
177 }
178 else
179 {
180 return null;
181 }
182 }
183
184 /// <summary>
185 /// 判断字符串的值是否为double,是:true;否:false
186 /// </summary>
187 /// <param name="s"></param>
188 /// <returns></returns>
189 public static bool IsDouble(this string s)
190 {
191 double d;
192 return double.TryParse(s, out d);
193 }
194
195 /// <summary>
196 /// 将字符串转换为double
197 /// </summary>
198 /// <param name="s"></param>
199 /// <returns></returns>
200 public static double ToDouble(this string s)
201 {
202 return double.Parse(s);
203 }
204
205 public static double? ToDoubleOrDefault(this string s)
206 {
207 if (s.IsDouble()&& !s.IsNullOrEmpty())
208 {
209 return double.Parse(s);
210 }
211 else
212 {
213 return null;
214 }
215 }
216
217 /// <summary>
218 /// 判断是否为空或0,是返回null;否返回string
219 /// </summary>
220 /// <param name="d"></param>
221 /// <returns></returns>
222 public static string IsNullDecimal(this decimal? d)
223 {
224 if (d.HasValue&&d.Value>0)
225 {
226 return d.Value.ToString();
227 }
228 else
229 {
230 return null;
231 }
232 }
233
234 /// <summary>
235 /// 转换成decimal 不成功返回 null
236 /// </summary>
237 /// <param name="d"></param>
238 /// <returns></returns>
239 public static decimal? ToDecimal(this string d)
240 {
241 if (!d.IsNullOrEmpty() && d.IsDecimal())
242 {
243 return decimal.Parse(d);
244 }
245 else
246 {
247 return null;
248 }
249 }
250
251 /// <summary>
252 /// 是否为decimal类型
253 /// </summary>
254 /// <param name="s"></param>
255 /// <returns></returns>
256 public static bool IsDecimal(this string s)
257 {
258 decimal d;
259 return decimal.TryParse(s,out d);
260 }
261
262
276
277 }