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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » String ReplaceScreenshots 
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.