Removes the a given prefix once from a string but only if the string starts with the prefix. - CSharp System

CSharp examples for System:String Strip

Description

Removes the a given prefix once from a string but only if the string starts with the prefix.

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*from  w w  w .j  a va 2s .  c  o m*/

public class Main{
        /// <summary>
		/// Removes the a given prefix once from a string but only if the string starts with the prefix.
		/// </summary>
		/// <param name="source">The string that may contain the prefix.</param>
		/// <param name="prefix">The prefix to remove.</param>
		/// <returns>The source string without the prefix.</returns>
		public static string RemovePrefixOnce(this string source, string prefix)
		{
			if (source == null)
			{
				throw new ArgumentNullException("source");
			}
			if (prefix == null)
			{
				throw new ArgumentNullException("prefix");
			}
			if (source.StartsWith(prefix))
			{
				source = source.Substring(prefix.Length);
			}
			return source;
		}
}

Related Tutorials