Return the substring up to but not including the first instance of 'c'. - CSharp System

CSharp examples for System:String SubString

Description

Return the substring up to but not including the first instance of 'c'.

Demo Code


using System;//w  ww .j a v a 2  s  .co  m

public class Main{
        /// <summary>
        /// Return the substring up to but not including the first instance of 'c'.
        /// </summary>
        /// <param name="src">The source string.</param>
        /// <param name="c">The character to look for.</param>
        /// <returns>The substring up to but not including the first instance of 'c'. If 'c' is not found, the entire string is returned.</returns>
		public static string SubstringBefore (this String src, char c) {
			if (String.IsNullOrEmpty(src)) return "";
 
			int idx = Math.Min(src.Length, src.IndexOf(c));
			if (idx < 0) return src;
			return src.Substring(0, idx);
		}
}

Related Tutorials