remove any of a set of chars from a given string.

   
 
using System;
public class Class1 {
    public static void Main(string[] strings) {
        char[] cSpecialChars = { '
', ',', '_' };
        string s = "_This is, a str
ing";
        Console.WriteLine(RemoveSpecialChars(s, cSpecialChars));
    }
    public static string RemoveSpecialChars(string sInput,char[] cTargets) {
        string sOutput = sInput;
        foreach (char c in cTargets) {
            for (; ; ) {
                int nOffset = sOutput.IndexOf(c);
                if (nOffset == -1) {
                    break;
                }
                string sBefore = sOutput.Substring(0, nOffset);
                string sAfter = sOutput.Substring(nOffset + 1);
                sOutput = sBefore + sAfter;
            }
        }
        return sOutput;
    }
}

    


String insert and output


   


using System;

public class StringTest
{
    public static void Main()
    {
        string test1 = "This is a test string";
        string test2, test3;

        test2 = test1.Insert(15, "application ");
        test3 = test1.ToUpper();

        Console.WriteLine("test1: '{0}'", test1);
        Console.WriteLine("test2: '{0}'", test2);
        Console.WriteLine("test3: '{0}'", test3);

        if (test1 == test3)
            Console.WriteLine("test1 is equal to test3");
        else
            Console.WriteLine("test1 is not equal to test3");
        
        test2 = test1.Replace("test", "sample");
        Console.WriteLine("the new test2: '{0}'", test2);

    }
}


           
          


Inserting, replacing, and removing


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Inserting, replacing, and removing. 
  
using System;  
  
public class InsRepRevDemo {  
  public static void Main() {  
    string str = "This test"; 
 
    Console.WriteLine("Original string: " + str); 
     
    // Insert 
    str = str.Insert(5, "is a "); 
    Console.WriteLine(str); 
 
    // Replace string 
    str = str.Replace("is", "was"); 
    Console.WriteLine(str); 
 
    // Replace characters 
    str = str.Replace('a', 'X'); 
    Console.WriteLine(str); 
 
    // Remove 
    str = str.Remove(4, 5); 
    Console.WriteLine(str); 
  } 
}

           
          


String reverse and replace


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


 
using System; 
 
// Declare a delegate.  
delegate void strMod(ref string str); 
 
public class StringOps { 
  // Replaces spaces with hyphens. 
  static void replaceSpaces(ref string a) { 
    Console.WriteLine("Replaces spaces with hyphens."); 
    a = a.Replace(' ', '-'); 
  }  
 
  // Remove spaces. 
  static void removeSpaces(ref string a) { 
    string temp = ""; 
    int i; 
 
    Console.WriteLine("Removing spaces."); 
    for(i=0; i < a.Length; i++) 
      if(a&#91;i&#93; != &#039; &#039;) temp += a&#91;i&#93;; 
 
    a = temp; 
  }  
 
  // Reverse a string. 
  static void reverse(ref string a) { 
    string temp = ""; 
    int i, j; 
 
    Console.WriteLine("Reversing string."); 
    for(j=0, i=a.Length-1; i >= 0; i--, j++) 
      temp += a[i]; 
 
    a = temp; 
  } 
     
  public static void Main() {  
    // Construct delegates. 
    strMod strOp; 
    strMod replaceSp = new strMod(replaceSpaces); 
    strMod removeSp = new strMod(removeSpaces); 
    strMod reverseStr = new strMod(reverse); 
    string str = "This is a test"; 
 
    // Set up multicast. 
    strOp = replaceSp; 
    strOp += reverseStr; 
 
    // Call multicast. 
    strOp(ref str); 
    Console.WriteLine("Resulting string: " + str); 
    Console.WriteLine(); 
     
    // Remove replace and add remove. 
    strOp -= replaceSp; 
    strOp += removeSp; 
 
    str = "This is a test."; // reset string 
 
    // Call multicast. 
    strOp(ref str); 
    Console.WriteLine("Resulting string: " + str); 
    Console.WriteLine(); 
  } 
}


           
          


use the Insert(), Remove(), and Replace() methods to modify strings

   
 

using System;

class MainClass {

    public static void Main() {

        string[] myStrings = {"To", "be", "or", "not","to", "be"};
        string myString = String.Join(".", myStrings);    
                
        string myString10 = myString.Insert(6, "friends, ");
        Console.WriteLine("myString.Insert(6,"friends, ") = " + myString10);
        string myString11 = myString10.Remove(14, 7);
        Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
        string myString12 = myString11.Replace(&#039;,&#039;, &#039;?&#039;);
        Console.WriteLine("myString11.Replace(&#039;,&#039;, &#039;?&#039;) = " + myString12);
        string myString13 =myString12.Replace("to be", "Or not to be friends");
        Console.WriteLine("myString12.Replace("to be","Or not to be friends") = " + myString13);
    
    }
}

    


Replace char inside a string

   
 

using System;

public class TestStringsApp {
    public static void Main(string[] args) {
        string a = "strong";

        // Replace all &#039;o&#039; with &#039;i&#039;
        string b = a.Replace(&#039;o&#039;, &#039;i&#039;);
        Console.WriteLine(b);

        string c = b.Insert(3, "engthen");
        string d = c.ToUpper();
        Console.WriteLine(d);
    }
}

    


Formats a string to the current culture.

   
 

//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System.Globalization;

namespace Mindscape.BackgroundMotion.Core.Utilities
{
  /// <summary>
  /// A helper class which provides utilities for working with strings
  /// </summary>
  public static class StringUtils
  {
    /// <summary>
    /// Formats a string to the current culture.
    /// </summary>
    /// <param name="formatString">The format string.</param>
    /// <param name="objects">The objects.</param>
    /// <returns></returns>
    public static string FormatCurrentCulture(string formatString, params object[] objects)
    {
      return string.Format(CultureInfo.CurrentCulture, formatString, objects);
    }
  }
}