/// <summary> /// Returns a string that consists of the rightmost n characters of the source string /// </summary> /// <param name="source"></param> /// <param name="length"></param> /// <returns></returns> public static string Right(this string source, int length, OutOfRangeAction outOfRangeAction = OutOfRangeAction.returnNull) { if (length > source.Length || length < 0) { return(source.HandleOutOfRangeAction(outOfRangeAction)); } return(source.Substring(source.Length - length)); }
private static string HandleOutOfRangeAction(this string source, OutOfRangeAction outOfRangeAction) { switch (outOfRangeAction) { case OutOfRangeAction.returnNull: return(null); case OutOfRangeAction.returnEmpty: return(String.Empty); case OutOfRangeAction.throwException: throw new IndexOutOfRangeException("index is not in range"); default: throw new NotImplementedException($"{outOfRangeAction} is not handled"); } }