CSharp examples for System:String Strip
Removes all invalid file name characters (\ : * ? " < > |) from a string.
// Copyright ? 2011, Grid Protection Alliance. All Rights Reserved. using System.Text.RegularExpressions; using System.Text; using System.IO;/*ww w. j a v a 2 s. c om*/ using System.Globalization; using System.ComponentModel; using System.Collections.Generic; using System; public class Main{ /// <summary> /// Removes all invalid file name characters (\ / : * ? " < > |) from a string. /// </summary> /// <param name="value">Input string.</param> /// <returns>Returns <paramref name="value" /> with all invalid file name characters removed.</returns> public static string RemoveInvalidFileNameCharacters(this string value) { return value.RemoveCharacters(c => Array.IndexOf(Path.GetInvalidFileNameChars(), c) >= 0); } /// <summary> /// Removes all characters passing delegate test from a string. /// </summary> /// <param name="value">Input string.</param> /// <param name="characterTestFunction">Delegate used to determine whether or not character should be removed.</param> /// <returns>Returns <paramref name="value" /> with all characters passing delegate test removed.</returns> public static string RemoveCharacters(this string value, Func<char, bool> characterTestFunction) { // <pex> if (characterTestFunction == (Func<char, bool>)null) throw new ArgumentNullException("characterTestFunction"); // </pex> if (string.IsNullOrEmpty(value)) return ""; StringBuilder result = new StringBuilder(); char character; for (int x = 0; x < value.Length; x++) { character = value[x]; if (!characterTestFunction(character)) result.Append(character); } return result.ToString(); } }