/// <summary> /// Extension Method: /// This method returns a String that is contained in the string between the two separator. /// example: /// <code> "valueOut(valueIn)valueExt".BetweenFirst("(", ")",StringManipulationMode.Exclusive);</code> /// will return a String with: /// <code> /// valueIn /// </code> /// /// </summary> /// <param name="str">the string to act on</param> /// <param name="startSeparator">the left separator</param> /// <param name="endSeparator">the right separator</param> /// <param name="mode">exclude or include the separators</param> /// <returns>the extrated string</returns> public static String BetweenFirst(this String str, String from, String to, StringManipulationMode mode) { String result = null; int firstIndex = str.IndexOf(from); int lastIndex = str.IndexOf(to); if(lastIndex < 0 || firstIndex < 0 || lastIndex < firstIndex) { return result; } if(mode == StringManipulationMode.Exclusive){ result = SubStringFrom(str, firstIndex + from.Length + 1, lastIndex); } else result = SubStringFrom(str, firstIndex + 1 , lastIndex + to.Length); return result; }
/// <summary> /// Extension Method: /// This method return the substring after a separator /// example: /// <code> "before.value".BeforeLast('.', StringManipulationMode.Exclusive);</code> /// will return a String with: /// <code> /// before /// </code> /// /// </summary> /// <param name="str">the string to act on</param> /// <param name="from">the separator</param> /// <param name="mode">exclude or include the separators</param> /// <returns>the new string</returns> public static string AfterLast(this string str, char from, StringManipulationMode mode) { if (str == null) { return str; } int startindex = (str).LastIndexOf(from); if (startindex < 0) { return str; } string result; if (mode == StringManipulationMode.Exclusive) { result = str.Remove(0, startindex + 1); } else { result = str.Remove(0, startindex); } return result; }
/// <summary> /// Extension Method: /// This method delete a substring between two separators and returns a new String. /// example: /// <code> "(one)(two)(three)".EraseBetweenFirst('(', ')', StringManipulationMode.Exclusive);</code> /// will return a String with: /// <code> /// ()(two)(three) /// </code> /// /// </summary> /// <param name="str">the string to act on</param> /// <param name="from">the left separator</param> /// <param name="to">the right separator</param> /// <param name="mode">exclude or include the separators</param> /// <returns>the new string</returns> public static string EraseBetweenFirst(this string str, char from, char to, StringManipulationMode mode) { if (str.IsNullOrEmpty()) { return str; } int startindex = (str).IndexOf(from); if (startindex < 0) { return str; } int endIndex = str.IndexOf(to); string result = null; if(mode == StringManipulationMode.Exclusive) { result = str.Remove(startindex + 1, (endIndex + 1) - (startindex + 2)); } else { result = str.Remove(startindex, (endIndex + 1 ) - (startindex)); } return result; }
/// <summary> /// Extension Method: /// This method return the substring before a separator /// example: /// <code> "before.value".BeforeLast('.', StringManipulationMode.Exclusive);</code> /// will return a String with: /// <code> /// before /// </code> /// /// </summary> /// <param name="str">the string to act on</param> /// <param name="from">the separator</param> /// <param name="mode">exclude or include the separators</param> /// <returns>the new string</returns> public static string BeforeLast(this string str, char from, StringManipulationMode mode) { int startindex = str.LastIndexOf(from); if (startindex < 0) { return str; } string result; if(mode == StringManipulationMode.Exclusive){ result = str.Substring(0, startindex); } else { result = str.Substring(0, startindex+1); } return result; }
public static String SubStringFrom(this String str, int from, int to, StringManipulationMode mode) { if (str.Length < to || from > to) { throw new ArgumentException("from must be less or equal than to and string length must be greater or equal than to"); } String result = str; if (mode == StringManipulationMode.Inclusive) { result = str.Substring(from - 1, to + 1 - from); } else if (mode == StringManipulationMode.Exclusive) { result = str.Substring(from, to -1 - from); } else { throw new ArgumentException(mode + " is not yet supported"); } return result; }