Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
568
|
Chapter 10: Regular Expressions
To use the overloaded Replace methods to replace the word "FOO" with the word
"BAR" in a sentence, you could write the following:
public static void TestReplace( )
{
string source = "Replace the FOO in this text block of text FOO.";
string matchPattern = "FOO";
string replaceStr = "BAR";
Console.WriteLine(Replace(source, matchPattern, replaceStr));
Console.WriteLine(Replace(source, matchPattern, replaceStr, -1));
Console.WriteLine(Replace(source, matchPattern, replaceStr, -1, 0));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1, 0));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1, 20));
Console.WriteLine(Replace(source, matchPattern, replaceStr, -1, 0));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1, 0));
Console.WriteLine(Replace(source, matchPattern, replaceStr, 1, 20));
}
which would produce the following output:
Replace the BAR in this text block of text BAR.
Replace the BAR in this text block of text BAR.
Replace the BAR in this text block of text BAR.
Replace the BAR in this text block of text FOO.
Replace the BAR in this text block of text FOO.
Replace the BAR in this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata