Returns the part of the string before the first occurrence of the given string, or null when the original text does not contain the text to find. - CSharp System

CSharp examples for System:String Contain

Description

Returns the part of the string before the first occurrence of the given string, or null when the original text does not contain the text to find.

Demo Code


using System;/*from   www . j  ava 2 s  .c om*/

public class Main{
        /// <summary>
        /// Returns the part of the string before the first occurrence of the given string,
        /// or null when the original text does not contain the text to find. 
        /// </summary>
        public static string BeforeFirst(this string originalText, string textToFind)
        {
            if (String.IsNullOrEmpty(originalText) || !originalText.Contains(textToFind))
            {
                return null;
            }

            return originalText.Substring(0, originalText.IndexOf(textToFind, StringComparison.Ordinal));
        }
}

Related Tutorials