Get the head of a string non destructive, ie leaves the text as it was case sensitive - CSharp System

CSharp examples for System:String Case

Description

Get the head of a string non destructive, ie leaves the text as it was case sensitive

Demo Code


using System.Text;
using System;/*from  ww  w  .  j  a va2  s  . c  o  m*/

public class Main{
        /// <summary>
        ///   Get the head of a string
        /// 
        ///   non destructive, ie leaves the text as it was
        ///   case sensitive
        /// </summary>
        /// <param name = "text">Text string</param>
        /// <param name = "uptoItem">Head cut of point (neck)</param>
        /// <returns>Head only</returns>
        public static string Head(this string text, string uptoItem)
        {
            return Head(text, uptoItem, StringComparison.CurrentCulture);
        }
        /// <summary>
        ///   Get the head of a string
        /// 
        ///   non destructive, ie leaves the text as it was
        /// </summary>
        /// <param name = "text">Text string</param>
        /// <param name = "uptoItem">Head cut of point (neck)</param>
        /// <param name = "comparisonType">String Comparison</param>
        /// <returns>Head only</returns>
        public static string Head(this string text, string uptoItem, StringComparison comparisonType)
        {
            var position = text.IndexOf(uptoItem, comparisonType);
            string headText;

            if (position == -1)
            {
                headText = text;
            }
            else
            {
                headText = text.Substring(0, position);
            }

            return headText;
        }
        /// <summary>
        ///   Get the head of a string, 
        /// 
        ///   destructive, ie leaving only the body in the text variable
        ///   case sensitive
        /// </summary>
        /// <param name = "text">Text string</param>
        /// <param name = "uptoItem">Head cut of point (neck)</param>
        /// <returns>Head only</returns>
        public static string Head(ref string text, string uptoItem)
        {
            return Head(ref text, uptoItem, StringComparison.CurrentCulture);
        }
        /// <summary>
        ///   Get the head of a string, 
        /// 
        ///   destructive, ie leaving only the body in the text variable
        /// </summary>
        /// <param name = "text">Text string</param>
        /// <param name = "uptoItem">Head cut of point (neck)</param>
        /// <param name = "comparisonType">String Comparison</param>
        /// <returns>Head only</returns>
        public static string Head(ref string text, string uptoItem, StringComparison comparisonType)
        {
            var position = text.IndexOf(uptoItem, comparisonType);
            string headText;

            if (position == -1)
            {
                headText = text;
                text = "";
            }
            else
            {
                headText = text.Substring(0, position);
                text = text.Substring(position + uptoItem.Length);
            }

            return headText;
        }
}

Related Tutorials