String reverse and replace : String Replace « Data Types « C# / C Sharp






String reverse and replace

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[i] != ' ') temp += a[i]; 
 
    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(); 
  } 
}


           
         
    
    
  








Related examples in the same category

1.Replace char inside a string
2.use the Insert(), Remove(), and Replace() methods to modify strings
3.Inserting, replacing, and removingInserting, replacing, and removing
4.String insert and outputString insert and output
5.remove any of a set of chars from a given string.
6.Replaces the specified string to replace.
7.Replace Once
8.Replaces the new lines in a string with the given replacement characters.