Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Text;
- // ReSharper disable once CheckNamespace
- namespace SakuraWorks.Commons.Extensions
- {
- public static class StringExtensions
- {
- /// <summary>
- /// 文字列の指定した位置から指定した長さを取得する
- /// <code>
- /// string s = null; s.Mid(*, *) => null
- /// "abc".Mid(*, -ve) => ""
- /// *.Mid(0, *) => ""
- /// "abc".Mid(0, 2) => "ab"
- /// "abc".Mid(0, 4) => "abc"
- /// "abc".Mid(2, 4) => "c"
- /// "abc".Mid(4, 2) => ""
- /// "abc".Mid(-2, 2) => "ab"
- /// </code>
- /// </summary>
- /// <param name="str">文字列</param>
- /// <param name="start">開始位置(先頭0)</param>
- /// <param name="len">長さ</param>
- /// <returns>取得した文字列</returns>
- public static string Mid(this string str, int start, int len = int.MaxValue)
- {
- if (str == null)
- return null;
- if (len < 0 || start > str.Length)
- return String.Empty;
- if (start < 0)
- start = 0;
- if (str.Length - start <= len)
- return str.Substring(start);
- return str.Substring(start, len);
- }
- /// <summary>
- /// 文字列の先頭から指定した長さの文字列を取得する
- /// <code>
- /// string s = null; s.Left(*) => null
- /// *.Left(-ve) => ""
- /// "".Left(*) => ""
- /// "abc".Left(0) => ""
- /// "abc".Left(2) => "ab"
- /// "abc".Left(4) => "abc"
- /// </code>
- /// </summary>
- /// <param name="str">文字列</param>
- /// <param name="len">長さ</param>
- /// <returns>取得した文字列</returns>
- public static string Left(this string str, int len)
- {
- if (str == null)
- return null;
- if (len < 0)
- return String.Empty;
- if (str.Length <= len)
- return str;
- return str.Substring(0, len);
- }
- /// <summary>
- /// 文字列の末尾から指定した長さの文字列を取得する
- /// <code>
- /// string s = null; s.Left(*) => null
- /// *.Left(-ve) => ""
- /// "".Left(*) => ""
- /// "abc".Left(0) => ""
- /// "abc".Left(2) => "bc"
- /// "abc".Left(4) => "abc"
- /// </code>
- /// </summary>
- /// <param name="str">文字列</param>
- /// <param name="len">長さ</param>
- /// <returns>取得した文字列</returns>
- public static string Right(this string str, int len)
- {
- if (str == null)
- return null;
- if (len < 0)
- return String.Empty;
- if (str.Length <= len)
- return str;
- return str.Substring(str.Length - len, len);
- }
- public static string Capitalize(this string str)
- {
- if (str.IsNullOrWhiteSpace())
- return str;
- var firstChar = str[0];
- if (char.IsUpper(firstChar))
- return str;
- return new StringBuilder()
- .Append(char.ToUpperInvariant(firstChar))
- .Append(str.Substring(1))
- .ToString();
- }
- public static string Uncapitalize(this string str)
- {
- if (str.IsNullOrWhiteSpace())
- return str;
- var firstChar = str[0];
- if (char.IsLower(firstChar))
- return str;
- return new StringBuilder()
- .Append(char.ToLowerInvariant(firstChar))
- .Append(str.Substring(1))
- .ToString();
- }
- /// <summary>
- /// 指定された文字列がnullまたはEmptyであるかどうかを示します
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsNullOrEmpty(this string str)
- {
- return String.IsNullOrEmpty(str);
- }
- /// <summary>
- /// 指定された文字列がnullまたはEmptyではないかどうかを示します
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsNotNullAndEmpty(this string str)
- {
- return !String.IsNullOrEmpty(str);
- }
- /// <summary>
- /// 指定された文字列がnullまたはEmptyであるか、空白文字だけであるかどうかを示します
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsNullOrWhiteSpace(this string str)
- {
- if (str == null) return true;
- for (int i = 0; i < str.Length; i++)
- {
- if (!Char.IsWhiteSpace(str[i])) return false;
- }
- return true;
- }
- /// <summary>
- /// 指定された文字列がnullまたはEmptyであるか、空白文字だけではないかどうかを示します
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsNotNullAndWhiteSpace(this string str)
- {
- return !IsNullOrWhiteSpace(str);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="str"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string IfNullOrEmpty(this string str, string value)
- {
- return IsNullOrEmpty(str) ? value : str;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="str"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string IfNullOrWhiteSpace(this string str, string value)
- {
- return IsNullOrWhiteSpace(str) ? value : str;
- }
- public static int? ParseIntNullable(this string str)
- {
- if (str == null)
- return null;
- int result;
- if (int.TryParse(str, out result))
- return result;
- else
- return null;
- }
- public static DateTime? ParseDateTimeNullable(this string str)
- {
- if (str == null)
- return null;
- DateTime result;
- if (DateTime.TryParseExact(str, new[] { "yyyy/M/d" }, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None,
- out result))
- return result;
- else
- return null;
- }
- /// <summary>
- /// 引用符で囲む
- /// 文字列中の引用符は引用符2つに変換
- /// </summary>
- /// <param name="str"></param>
- /// <param name="quote"></param>
- /// <returns></returns>
- public static string Quoted(this string str, char quote = '"')
- {
- if (str == null)
- return null;
- var result = new StringBuilder();
- result.Append(quote);
- result.Append(str.Replace(new string(quote, 1), new string(quote, 2)));
- result.Append(quote);
- return result.ToString();
- }
- public static string TrimToNull(this string str, params char[] trimChars)
- {
- if (str == null)
- return null;
- var result = str.Trim(trimChars);
- return result.Length == 0 ? null : result;
- }
- public static string TrimEndToNull(this string str, params char[] trimChars)
- {
- if (str == null)
- return null;
- var result = str.TrimEnd(trimChars);
- return result.Length == 0 ? null : result;
- }
- private static readonly Encoding LocalEncoding = Encoding.GetEncoding("Shift_JIS");
- /// <summary>
- /// 指定したエンコードにおいてバイト単位で
- /// 文字列の指定した位置から指定した長さを取得する
- /// <code>
- /// // Shift_JISの場合
- /// string s = null; s.MidInByte(*, *) => null
- /// "abc".MidInByte(0, 2) => "ab"
- /// "abc".MidInByte(0, 4) => "abc"
- /// "あいう".MidInByte(0, 4) => "あい"
- /// "あいう".MidInByte(1, 2) => ""
- /// "あいう".MidInByte(1, 3) => "い"
- /// "あいう".MidInByte(1, 4) => "い"
- /// "あいう".MidInByte(1, 5) => "いう"
- /// "あいう".MidInByte(1, 6) => "いう"
- /// "あいう".MidInByte(7, 2) => ""
- /// </code>
- /// </summary>
- /// <param name="str">文字列</param>
- /// <param name="start">開始位置(先頭0) バイト単位</param>
- /// <param name="len">長さ バイト単位</param>
- /// <param name="encoding"></param>
- /// <returns></returns>
- public static string MidInByte(this string str, int start, int len = int.MaxValue, Encoding encoding = null)
- {
- if (str == null)
- return null;
- if (encoding == null)
- encoding = LocalEncoding;
- var result = new StringBuilder();
- var i = 0;
- foreach (var c in str.ToCharArray())
- {
- var n = encoding.GetByteCount(new[] { c });
- if (start <= i && i + n <= start + len)
- result.Append(c);
- i += n;
- if (i > start + len)
- break;
- }
- return result.Length <= 0 ? string.Empty : result.ToString();
- }
- }
- }
Add Comment
Please, Sign In to add comment