CSharp examples for System:String Start End
Ensures a string starts with a specific string.
// Copyright ? 2011, Grid Protection Alliance. All Rights Reserved. using System.Text.RegularExpressions; using System.Text; using System.IO;//from w ww. j a va 2 s. c om using System.Globalization; using System.ComponentModel; using System.Collections.Generic; using System; public class Main{ /// <summary> /// Ensures a string starts with a specific string. /// </summary> /// <param name="value">Input string to process.</param> /// <param name="startString">The string desired at string start.</param> /// <returns>The sent string with string at the start.</returns> public static string EnsureStart(this string value, string startString) { if (string.IsNullOrEmpty(value)) return ""; if (string.IsNullOrEmpty(startString)) return value; if (value.IndexOf(startString) == 0) return value; else return string.Concat(startString, value); } /// <summary> /// Ensures a string starts with a specific character. /// </summary> /// <param name="value">Input string to process.</param> /// <param name="startChar">The character desired at string start.</param> /// <param name="removeRepeatingChar">Set to <c>true</c> to ensure one and only one instance of <paramref name="startChar"/>.</param> /// <returns>The sent string with character at the start.</returns> public static string EnsureStart(this string value, char startChar, bool removeRepeatingChar) { if (string.IsNullOrEmpty(value)) return ""; if (startChar == 0) return value; if (value[0] == startChar) { if (removeRepeatingChar) return value.Substring(LastIndexOfRepeatedChar(value, startChar, 0)); return value; } else return string.Concat(startChar, value); } /// <summary> /// Ensures a string starts with a specific character. /// </summary> /// <param name="value">Input string to process.</param> /// <param name="startChar">The character desired at string start.</param> /// <returns>The sent string with character at the start.</returns> public static string EnsureStart(this string value, char startChar) { return EnsureStart(value, startChar, false); } }