Guest User

Untitled

a guest
Dec 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4.  
  5. // ReSharper disable once CheckNamespace
  6. namespace SakuraWorks.Commons.Extensions
  7. {
  8. public static class StringExtensions
  9. {
  10. /// <summary>
  11. /// 文字列の指定した位置から指定した長さを取得する
  12. /// <code>
  13. /// string s = null; s.Mid(*, *) => null
  14. /// "abc".Mid(*, -ve) => ""
  15. /// *.Mid(0, *) => ""
  16. /// "abc".Mid(0, 2) => "ab"
  17. /// "abc".Mid(0, 4) => "abc"
  18. /// "abc".Mid(2, 4) => "c"
  19. /// "abc".Mid(4, 2) => ""
  20. /// "abc".Mid(-2, 2) => "ab"
  21. /// </code>
  22. /// </summary>
  23. /// <param name="str">文字列</param>
  24. /// <param name="start">開始位置(先頭0)</param>
  25. /// <param name="len">長さ</param>
  26. /// <returns>取得した文字列</returns>
  27. public static string Mid(this string str, int start, int len = int.MaxValue)
  28. {
  29. if (str == null)
  30. return null;
  31.  
  32. if (len < 0 || start > str.Length)
  33. return String.Empty;
  34. if (start < 0)
  35. start = 0;
  36. if (str.Length - start <= len)
  37. return str.Substring(start);
  38. return str.Substring(start, len);
  39. }
  40.  
  41. /// <summary>
  42. /// 文字列の先頭から指定した長さの文字列を取得する
  43. /// <code>
  44. /// string s = null; s.Left(*) => null
  45. /// *.Left(-ve) => ""
  46. /// "".Left(*) => ""
  47. /// "abc".Left(0) => ""
  48. /// "abc".Left(2) => "ab"
  49. /// "abc".Left(4) => "abc"
  50. /// </code>
  51. /// </summary>
  52. /// <param name="str">文字列</param>
  53. /// <param name="len">長さ</param>
  54. /// <returns>取得した文字列</returns>
  55. public static string Left(this string str, int len)
  56. {
  57. if (str == null)
  58. return null;
  59.  
  60. if (len < 0)
  61. return String.Empty;
  62. if (str.Length <= len)
  63. return str;
  64. return str.Substring(0, len);
  65. }
  66.  
  67. /// <summary>
  68. /// 文字列の末尾から指定した長さの文字列を取得する
  69. /// <code>
  70. /// string s = null; s.Left(*) => null
  71. /// *.Left(-ve) => ""
  72. /// "".Left(*) => ""
  73. /// "abc".Left(0) => ""
  74. /// "abc".Left(2) => "bc"
  75. /// "abc".Left(4) => "abc"
  76. /// </code>
  77. /// </summary>
  78. /// <param name="str">文字列</param>
  79. /// <param name="len">長さ</param>
  80. /// <returns>取得した文字列</returns>
  81. public static string Right(this string str, int len)
  82. {
  83. if (str == null)
  84. return null;
  85.  
  86. if (len < 0)
  87. return String.Empty;
  88. if (str.Length <= len)
  89. return str;
  90. return str.Substring(str.Length - len, len);
  91. }
  92.  
  93. public static string Capitalize(this string str)
  94. {
  95. if (str.IsNullOrWhiteSpace())
  96. return str;
  97.  
  98. var firstChar = str[0];
  99. if (char.IsUpper(firstChar))
  100. return str;
  101.  
  102. return new StringBuilder()
  103. .Append(char.ToUpperInvariant(firstChar))
  104. .Append(str.Substring(1))
  105. .ToString();
  106. }
  107.  
  108. public static string Uncapitalize(this string str)
  109. {
  110. if (str.IsNullOrWhiteSpace())
  111. return str;
  112.  
  113. var firstChar = str[0];
  114. if (char.IsLower(firstChar))
  115. return str;
  116.  
  117. return new StringBuilder()
  118. .Append(char.ToLowerInvariant(firstChar))
  119. .Append(str.Substring(1))
  120. .ToString();
  121. }
  122.  
  123. /// <summary>
  124. /// 指定された文字列がnullまたはEmptyであるかどうかを示します
  125. /// </summary>
  126. /// <param name="str"></param>
  127. /// <returns></returns>
  128. public static bool IsNullOrEmpty(this string str)
  129. {
  130. return String.IsNullOrEmpty(str);
  131. }
  132.  
  133. /// <summary>
  134. /// 指定された文字列がnullまたはEmptyではないかどうかを示します
  135. /// </summary>
  136. /// <param name="str"></param>
  137. /// <returns></returns>
  138. public static bool IsNotNullAndEmpty(this string str)
  139. {
  140. return !String.IsNullOrEmpty(str);
  141. }
  142.  
  143. /// <summary>
  144. /// 指定された文字列がnullまたはEmptyであるか、空白文字だけであるかどうかを示します
  145. /// </summary>
  146. /// <param name="str"></param>
  147. /// <returns></returns>
  148. public static bool IsNullOrWhiteSpace(this string str)
  149. {
  150. if (str == null) return true;
  151.  
  152. for (int i = 0; i < str.Length; i++)
  153. {
  154. if (!Char.IsWhiteSpace(str[i])) return false;
  155. }
  156.  
  157. return true;
  158. }
  159.  
  160. /// <summary>
  161. /// 指定された文字列がnullまたはEmptyであるか、空白文字だけではないかどうかを示します
  162. /// </summary>
  163. /// <param name="str"></param>
  164. /// <returns></returns>
  165. public static bool IsNotNullAndWhiteSpace(this string str)
  166. {
  167. return !IsNullOrWhiteSpace(str);
  168. }
  169.  
  170. /// <summary>
  171. ///
  172. /// </summary>
  173. /// <param name="str"></param>
  174. /// <param name="value"></param>
  175. /// <returns></returns>
  176. public static string IfNullOrEmpty(this string str, string value)
  177. {
  178. return IsNullOrEmpty(str) ? value : str;
  179. }
  180.  
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. /// <param name="str"></param>
  185. /// <param name="value"></param>
  186. /// <returns></returns>
  187. public static string IfNullOrWhiteSpace(this string str, string value)
  188. {
  189. return IsNullOrWhiteSpace(str) ? value : str;
  190. }
  191.  
  192. public static int? ParseIntNullable(this string str)
  193. {
  194. if (str == null)
  195. return null;
  196.  
  197. int result;
  198. if (int.TryParse(str, out result))
  199. return result;
  200. else
  201. return null;
  202. }
  203.  
  204. public static DateTime? ParseDateTimeNullable(this string str)
  205. {
  206. if (str == null)
  207. return null;
  208.  
  209. DateTime result;
  210. if (DateTime.TryParseExact(str, new[] { "yyyy/M/d" }, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None,
  211. out result))
  212. return result;
  213. else
  214. return null;
  215. }
  216.  
  217. /// <summary>
  218. /// 引用符で囲む
  219. /// 文字列中の引用符は引用符2つに変換
  220. /// </summary>
  221. /// <param name="str"></param>
  222. /// <param name="quote"></param>
  223. /// <returns></returns>
  224. public static string Quoted(this string str, char quote = '"')
  225. {
  226. if (str == null)
  227. return null;
  228.  
  229. var result = new StringBuilder();
  230. result.Append(quote);
  231. result.Append(str.Replace(new string(quote, 1), new string(quote, 2)));
  232. result.Append(quote);
  233.  
  234. return result.ToString();
  235. }
  236.  
  237. public static string TrimToNull(this string str, params char[] trimChars)
  238. {
  239. if (str == null)
  240. return null;
  241.  
  242. var result = str.Trim(trimChars);
  243.  
  244. return result.Length == 0 ? null : result;
  245. }
  246.  
  247. public static string TrimEndToNull(this string str, params char[] trimChars)
  248. {
  249. if (str == null)
  250. return null;
  251.  
  252. var result = str.TrimEnd(trimChars);
  253.  
  254. return result.Length == 0 ? null : result;
  255. }
  256.  
  257. private static readonly Encoding LocalEncoding = Encoding.GetEncoding("Shift_JIS");
  258.  
  259. /// <summary>
  260. /// 指定したエンコードにおいてバイト単位で
  261. /// 文字列の指定した位置から指定した長さを取得する
  262. /// <code>
  263. /// // Shift_JISの場合
  264. /// string s = null; s.MidInByte(*, *) => null
  265. /// "abc".MidInByte(0, 2) => "ab"
  266. /// "abc".MidInByte(0, 4) => "abc"
  267. /// "あいう".MidInByte(0, 4) => "あい"
  268. /// "あいう".MidInByte(1, 2) => ""
  269. /// "あいう".MidInByte(1, 3) => "い"
  270. /// "あいう".MidInByte(1, 4) => "い"
  271. /// "あいう".MidInByte(1, 5) => "いう"
  272. /// "あいう".MidInByte(1, 6) => "いう"
  273. /// "あいう".MidInByte(7, 2) => ""
  274. /// </code>
  275. /// </summary>
  276. /// <param name="str">文字列</param>
  277. /// <param name="start">開始位置(先頭0) バイト単位</param>
  278. /// <param name="len">長さ バイト単位</param>
  279. /// <param name="encoding"></param>
  280. /// <returns></returns>
  281. public static string MidInByte(this string str, int start, int len = int.MaxValue, Encoding encoding = null)
  282. {
  283. if (str == null)
  284. return null;
  285.  
  286. if (encoding == null)
  287. encoding = LocalEncoding;
  288.  
  289. var result = new StringBuilder();
  290. var i = 0;
  291. foreach (var c in str.ToCharArray())
  292. {
  293. var n = encoding.GetByteCount(new[] { c });
  294. if (start <= i && i + n <= start + len)
  295. result.Append(c);
  296.  
  297. i += n;
  298. if (i > start + len)
  299. break;
  300. }
  301. return result.Length <= 0 ? string.Empty : result.ToString();
  302. }
  303.  
  304. }
  305. }
Add Comment
Please, Sign In to add comment