CSharp examples for System:Char
Inserts a string into another string before each occurrence of the specified Character Type.
using System.Text; using System.Runtime.CompilerServices; using System.Reflection; using System.Linq; using System.Globalization; using System.Collections.Generic; using System;/*w w w.ja v a 2 s . c o m*/ public class Main{ /// <summary> /// Inserts a <code>string</code> into another <code>string</code> before each occurrence of the specified <see cref="CharacterType"/>. /// </summary> /// <param name="source">The <code>string</code> to insert into.</param> /// <param name="type">The <see cref="CharacterType"/> to insert before.</param> /// <param name="insert">The <code>string</code> to insert.</param> /// <returns>The resultant <code>string</code>.</returns> /// <remarks> /// In the case of the first character of <code>source</code> matching the specified <see cref="CharacterType"/>, the <code>insert</code> will not be inserted. /// </remarks> public static string InsertOnCharacter(this string source, CharacterType type, string insert) { var esb = new ExtendedStringBuilder(source.Length); var firstRun = false; foreach (var c in source) { var cType = c.GetCharacterType(); if (firstRun && cType == type) { esb += insert; } esb += c; firstRun = true; } return esb; } public static CharacterType GetCharacterType(this char c) { if (c >= 'a' && c <= 'z') { return CharacterType.LowerLetter; } if (c >= 'A' && c <= 'Z') { return CharacterType.UpperLetter; } if (c >= '0' && c <= '9') { return CharacterType.Number; } return CharacterType.Symbol; } }