Created
March 7, 2014 11:03
-
-
Save baba-s/9409584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text.RegularExpressions; | |
| /// <summary> | |
| /// String型の拡張メソッドを管理するクラス | |
| /// </summary> | |
| public static partial class StringExtensions | |
| { | |
| /// <summary> | |
| /// 指定した正規表現に一致する箇所が、指定した入力文字列内に見つかるかどうかを示します | |
| /// </summary> | |
| /// <param name="str">一致する対象を検索する文字列</param> | |
| /// <param name="pattern">一致させる正規表現パターン</param> | |
| /// <returns>正規表現と一致する対象が見つかった場合は true。それ以外の場合は false</returns> | |
| public static bool IsMatch(this string str, string pattern) | |
| { | |
| return Regex.IsMatch(str, pattern); | |
| } | |
| /// <summary> | |
| /// 指定した入力文字列内で、指定した正規表現に最初に一致する箇所を検索します | |
| /// </summary> | |
| /// <param name="str">一致する対象を検索する文字列</param> | |
| /// <param name="pattern">一致させる正規表現パターン</param> | |
| /// <returns>一致に関する情報を格納しているオブジェクト</returns> | |
| public static Match Match(this string str, string pattern) | |
| { | |
| return Regex.Match(str, pattern); | |
| } | |
| /// <summary> | |
| /// 指定した入力文字列内で、指定した正規表現に一致する箇所をすべて検索します | |
| /// </summary> | |
| /// <param name="str">一致する対象を検索する文字列</param> | |
| /// <param name="pattern">一致させる正規表現パターン</param> | |
| /// <returns>検索によって見つかった Match オブジェクトのコレクション</returns> | |
| public static MatchCollection Matches(this string str, string pattern) | |
| { | |
| return Regex.Matches(str, pattern); | |
| } | |
| /// <summary> | |
| /// 指定した入力文字列内で指定した正規表現に一致するすべての文字列を、指定した置換文字列に置換します | |
| /// </summary> | |
| /// <param name="str">一致する対象を検索する文字列</param> | |
| /// <param name="pattern">一致させる正規表現パターン</param> | |
| /// <param name="replacement">置換文字列</param> | |
| /// <returns>一致する各文字列が置換文字列に置き換えられる以外は入力文字列と同じである新しい文字列</returns> | |
| public static string ReplaceRegex(this string str, string pattern, string replacement) | |
| { | |
| return Regex.Replace(str, pattern, replacement); | |
| } | |
| /// <summary> | |
| /// コレクションを指定した文字で連結します | |
| /// </summary> | |
| /// <typeparam name="T">連結するコレクションの型</typeparam> | |
| /// <param name="source">連結するコレクション</param> | |
| /// <param name="separator">区切り記号として使用する文字列</param> | |
| /// <param name="format">使用する書式</param> | |
| /// <param name="provider">値の書式付けに使用するプロバイダー</param> | |
| /// <returns>コレクションを指定した文字で連結した文字列</returns> | |
| public static string ConcatWith<T>(this IEnumerable<T> source, string separator, string format, IFormatProvider provider = null) where T : IFormattable | |
| { | |
| return source.Select(x => x.ToString(format, provider)).Aggregate((a, b) => a + separator + b); | |
| } | |
| /// <summary> | |
| /// コレクションを指定した文字で連結します | |
| /// </summary> | |
| /// <typeparam name="T">連結するコレクションの型</typeparam> | |
| /// <param name="source">連結するコレクション</param> | |
| /// <param name="separator">区切り記号として使用する文字列</param> | |
| /// <returns>コレクションを指定した文字で連結した文字列</returns> | |
| public static string ConcatWith<T>(this IEnumerable<T> source, string separator) | |
| { | |
| return string.Join(separator, source.Select(c => c.ToString()).ToArray()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment