写代码时喜欢连贯的、链式的写下去,而不是光标再返回到前面写类型转换等等。
自定义了些字符串扩展方法:
1 /// <summary> 2 /// 字符串操作扩展方法类 3 /// </summary> 4 public static class StringOperationExtensions 5 { 6 /// <summary> 7 /// 若输入字符串为null,则返回String.Empty。 8 /// </summary> 9 /// <param name="s">输入字符串</param> 10 /// <returns>输入字符串或String.Empty</returns> 11 public static string EmptyIfNull(this string s) 12 { 13 return (s ?? string.Empty); 14 } 15 16 /// <summary> 17 /// 去除输入字符串两边的空白,若为null,则结果为string.Empty 18 /// </summary> 19 /// <param name="s">输入字符串</param> 20 /// <returns>去除两边空白的字符串或string.Empty</returns> 21 public static string TrimOrEmpty(this string s) 22 { 23 return s.EmptyIfNull().Trim(); 24 } 25 26 /// <summary> 27 /// 去除输入字符串两边的空白,若为null,则结果为null 28 /// </summary> 29 /// <param name="s">输入字符串</param> 30 /// <returns>去除两边空白的字符串或null</returns> 31 public static string TrimOrNull(this string s) 32 { 33 return (s == null ? null : s.Trim()); 34 } 35 36 /// <summary> 37 /// 将输入字符串转换成Int32,转换不成功则产生异常 38 /// </summary> 39 /// <param name="s">输入字符串</param> 40 /// <returns>Int32值</returns> 41 public static int ToInt32(this string s) 42 { 43 return Convert.ToInt32(s); 44 } 45 46 /// <summary> 47 /// 将输入字符串转换成Int32,转换不成功则返回提供的默认值,不产生异常 48 /// </summary> 49 /// <param name="s">输入字符串</param> 50 /// <param name="defaultValue">提供的默认值</param> 51 /// <returns>Int32值</returns> 52 public static int ToInt32OrDefault(this string s, int defaultValue) 53 { 54 int value; 55 return Int32.TryParse(s, out value) ? value : defaultValue; 56 } 57 58 /// <summary> 59 /// 将输入字符串转换成Int32,转换不成功返回0,不产生异常 60 /// </summary> 61 /// <param name="s">输入字符串</param> 62 /// <returns>Int32值</returns> 63 public static int ToInt32OrDefault(this string s) 64 { 65 return s.ToInt32OrDefault(0); 66 } 67 68 /// <summary> 69 /// 将输入字符串转换成decimal,转换不成功则产生异常 70 /// </summary> 71 /// <param name="s">输入字符串</param> 72 /// <returns>decimal值</returns> 73 public static decimal ToDecimal(this string s) 74 { 75 return Convert.ToDecimal(s); 76 } 77 78 /// <summary> 79 /// 将输入字符串转换成decimal,转换不成功则返回提供的默认值,不产生异常 80 /// </summary> 81 /// <param name="s">输入字符串</param> 82 /// <param name="defaultValue">提供的默认值</param> 83 /// <returns>decimal值或提供的默认值</returns> 84 public static decimal ToDecimalOrDefault(this string s, decimal defaultValue) 85 { 86 decimal value; 87 return decimal.TryParse(s, out value) ? value : defaultValue; 88 } 89 90 /// <summary> 91 /// 将输入字符串转换成decimal,转换不成功则返回0,不产生异常 92 /// </summary> 93 /// <param name="s">输入字符串</param> 94 /// <returns>decimal值或0</returns> 95 public static decimal ToDecimalOrDefault(this string s) 96 { 97 return s.ToDecimalOrDefault(0M); 98 } 99 100 /// <summary> 101 /// 将输入字符串转换成DateTime,转换不成功则产生异常 102 /// </summary> 103 /// <param name="s">输入字符串</param> 104 /// <returns>DateTime值</returns> 105 public static DateTime ToDateTime(this string s) 106 { 107 return Convert.ToDateTime(s); 108 } 109 110 /// <summary> 111 /// 将输入字符串转换成DateTime,转换不成功则产生异常 112 /// </summary> 113 /// <param name="s">输入字符串</param> 114 /// <param name="format">字符串格式</param> 115 /// <returns>DateTime值</returns> 116 public static DateTime ToDateTime(this string s, string format) 117 { 118 return DateTime.ParseExact(s, format, null); 119 } 120 121 /// <summary> 122 /// 将输入字符串转换成Int16,转换不成功则产生异常 123 /// </summary> 124 /// <param name="s">输入字符串</param> 125 /// <returns>Int16值</returns> 126 public static short ToInt16(this string s) 127 { 128 return Convert.ToInt16(s); 129 } 130 131 /// <summary> 132 /// 将输入字符串转换成Int16,转换不成功则返回提供的默认值,不产生异常 133 /// </summary> 134 /// <param name="s">输入字符串</param> 135 /// <param name="defaultValue">提供的默认值</param> 136 /// <returns>Int16值或提供的默认值</returns> 137 public static short ToInt16OrDefault(this string s, short defaultValue) 138 { 139 short value; 140 return short.TryParse(s, out value) ? value : defaultValue; 141 } 142 143 /// <summary> 144 /// 将输入字符串转换成Int16,转换不成功则返回0,不产生异常 145 /// </summary> 146 /// <param name="s">输入字符串</param> 147 /// <returns>Int16值或0</returns> 148 public static short ToInt16OrDefault(this string s) 149 { 150 return s.ToInt16OrDefault(0); 151 } 152 153 /// <summary> 154 /// 将输入字符串转换成Int64,转换不成功则产生异常 155 /// </summary> 156 /// <param name="s">输入字符串</param> 157 /// <returns>Int64值</returns> 158 public static long ToInt64(this string s) 159 { 160 return Convert.ToInt64(s); 161 } 162 163 /// <summary> 164 /// 将输入字符串转换成Int64,转换不成功则返回提供的默认值,不产生异常 165 /// </summary> 166 /// <param name="s">输入字符串</param> 167 /// <param name="defaultValue">提供的默认值</param> 168 /// <returns>Int64值或提供的默认值</returns> 169 public static long ToInt64OrDefault(this string s, long defaultValue) 170 { 171 long value; 172 return long.TryParse(s, out value) ? value : defaultValue; 173 } 174 175 /// <summary> 176 /// 将输入字符串转换成Int64,转换不成功则返回0,不产生异常 177 /// </summary> 178 /// <param name="s">输入字符串</param> 179 /// <returns>Int64值或0</returns> 180 public static long ToInt64OrDefault(this string s) 181 { 182 return s.ToInt64OrDefault(0); 183 } 184 185 /// <summary> 186 /// 将输入字符串转换成byte,转换不成功则产生异常 187 /// </summary> 188 /// <param name="s">输入字符串</param> 189 /// <returns>byte值</returns> 190 public static byte ToByte(this string s) 191 { 192 return Convert.ToByte(s); 193 } 194 195 /// <summary> 196 /// 将输入字符串转换成byte,转换不成功则返回提供的默认值,不产生异常 197 /// </summary> 198 /// <param name="s">输入字符串</param> 199 /// <param name="defaultValue">提供的默认值</param> 200 /// <returns>byte值或提供的默认值</returns> 201 public static byte ToByteOrDefault(this string s, byte defaultValue) 202 { 203 byte value; 204 return byte.TryParse(s, out value) ? value : defaultValue; 205 } 206 207 /// <summary> 208 /// 将输入字符串转换成byte,转换不成功则返回0,不产生异常 209 /// </summary> 210 /// <param name="s">输入字符串</param> 211 /// <returns>byte值或0</returns> 212 public static byte ToByteOrDefault(this string s) 213 { 214 return s.ToByteOrDefault(0); 215 } 216 217 /// <summary> 218 /// 将输入字符串转换成double,转换不成功则产生异常 219 /// </summary> 220 /// <param name="s">输入字符串</param> 221 /// <returns>double值</returns> 222 public static double ToDouble(this string s) 223 { 224 return Convert.ToDouble(s); 225 } 226 227 /// <summary> 228 /// 将输入字符串转换成double,转换不成功则返回提供的默认值,不产生异常 229 /// </summary> 230 /// <param name="s">输入字符串</param> 231 /// <param name="defaultValue">提供的默认值</param> 232 /// <returns>double值或提供的默认值</returns> 233 public static double ToDoubleOrDefault(this string s, double defaultValue) 234 { 235 double value; 236 return double.TryParse(s, out value) ? value : defaultValue; 237 } 238 239 /// <summary> 240 /// 将输入字符串转换成double,转换不成功则返回0,不产生异常 241 /// </summary> 242 /// <param name="s">输入字符串</param> 243 /// <returns>double值或0</returns> 244 public static double ToDoubleOrDefault(this string s) 245 { 246 return s.ToDoubleOrDefault(0D); 247 } 248 249 /// <summary> 250 /// 将输入字符串转换成float,转换不成功则产生异常 251 /// </summary> 252 /// <param name="s">输入字符串</param> 253 /// <returns>float值</returns> 254 public static float ToFloat(this string s) 255 { 256 return Convert.ToSingle(s); 257 } 258 259 /// <summary> 260 /// 将输入字符串转换成float,转换不成功则返回提供的默认值,不产生异常 261 /// </summary> 262 /// <param name="s">输入字符串</param> 263 /// <param name="defaultValue">提供的默认值</param> 264 /// <returns>float值或提供的默认值</returns> 265 public static float ToFloatOrDefault(this string s, float defaultValue) 266 { 267 float value; 268 return float.TryParse(s, out value) ? value : defaultValue; 269 } 270 271 /// <summary> 272 /// 将输入字符串转换成float,转换不成功则返回0,不产生异常 273 /// </summary> 274 /// <param name="s">输入字符串</param> 275 /// <returns>float值或0</returns> 276 public static float ToFloatOrDefault(this string s) 277 { 278 return s.ToFloatOrDefault(0F); 279 } 280 }
使用时就简单了:
1 int id = Request["id"].ToInt32(); 2 3 int itemId = Request["itemId"].ToInt32OrDefault();
时间: 2024-10-28 10:33:13