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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
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.
w__w_w.__j___a_v___a_2__s___._c_o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.