C#でMid、Left、Right関数

dobon.net

/// <summary>
/// 文字列の指定した位置から指定した長さを取得する
/// </summary>
/// <param name="str">文字列</param>
/// <param name="start">開始位置</param>
/// <param name="len">長さ</param>
/// <returns>取得した文字列</returns>
public static string Mid(string str, int start, int len)
{
    if (start <= 0)
    {
        throw new ArgumentException("引数'start'は1以上でなければなりません。");
    }
    if (len < 0)
    {
        throw new ArgumentException("引数'len'は0以上でなければなりません。");
    }
    if (str == null || str.Length < start)
    {
        return "";
    }
    if (str.Length < (start + len))
    {
        return str.Substring(start - 1);
    }
    return str.Substring(start - 1, len);
}

/// <summary>
/// 文字列の指定した位置から末尾までを取得する
/// </summary>
/// <param name="str">文字列</param>
/// <param name="start">開始位置</param>
/// <returns>取得した文字列</returns>
public static string Mid(string str, int start)
{
    return Mid(str, start, str.Length);
}

/// <summary>
/// 文字列の先頭から指定した長さの文字列を取得する
/// </summary>
/// <param name="str">文字列</param>
/// <param name="len">長さ</param>
/// <returns>取得した文字列</returns>
public static string Left(string str, int len)
{
    if (len < 0)
    {
        throw new ArgumentException("引数'len'は0以上でなければなりません。");
    }
    if (str == null)
    {
        return "";
    }
    if (str.Length <= len)
    {
        return str;
    }
    return str.Substring(0, len);
}

/// <summary>
/// 文字列の末尾から指定した長さの文字列を取得する
/// </summary>
/// <param name="str">文字列</param>
/// <param name="len">長さ</param>
/// <returns>取得した文字列</returns>
public static string Right(string str, int len)
{
    if (len < 0)
    {
        throw new ArgumentException("引数'len'は0以上でなければなりません。");
    }
    if (str == null)
    {
        return "";
    }
    if (str.Length <= len)
    {
        return str;
    }
    return str.Substring(str.Length - len, len);
}