CSharp examples for System:String Case
Remove the tail from text non destructive, ie leaves the text as it was case sensitive
using System.Text; using System;// w ww.j a v a 2 s .co m public class Main{ /// <summary> /// Remove the tail from text /// /// non destructive, ie leaves the text as it was /// case sensitive /// </summary> /// <param name = "text">Text string</param> /// <param name = "uptoItem">Tail cut off point</param> /// <returns>Tail only</returns> public static string Tail(this string text, string uptoItem) { return Tail(text, uptoItem, StringComparison.CurrentCulture); } /// <summary> /// Remove the tail from text /// /// non destructive, ie leaves the text as it was /// </summary> /// <param name = "text">Text string</param> /// <param name = "uptoItem">Tail cut off point</param> /// <param name = "comparisonType">String Comparison</param> /// <returns>Tail only</returns> public static string Tail(this string text, string uptoItem, StringComparison comparisonType) { var position = text.LastIndexOf(uptoItem, comparisonType); string tailText; if (position == -1) { tailText = text; } else { tailText = text.Substring(position + uptoItem.Length); } return tailText; } /// <summary> /// Remove the tail from text /// /// destructive, ie leaving only the body in the text variable /// case sensitive /// </summary> /// <param name = "text">Text string</param> /// <param name = "uptoItem">Tail cut off point</param> /// <returns>Tail only</returns> public static string Tail(ref string text, string uptoItem) { return Tail(ref text, uptoItem, StringComparison.CurrentCulture); } /// <summary> /// Remove the tail from text /// /// destructive, ie leaving only the body in the text variable /// </summary> /// <param name = "text">Text string</param> /// <param name = "uptoItem">Tail cut off point</param> /// <param name = "comparisonType">String Comparison</param> /// <returns>Tail only</returns> public static string Tail(ref string text, string uptoItem, StringComparison comparisonType) { var position = text.LastIndexOf(uptoItem, comparisonType); string tailText; if (position == -1) { tailText = text; text = ""; } else { tailText = text.Substring(position + uptoItem.Length); text = text.Substring(0, position); } return tailText; } }