Insert, Remove and Replace Methods of Class StringBuilder

Class StringBuilder provides 18 overloaded Insert methods to allow various types of data to be inserted at any position in a StringBuilder. The class provides versions for each of the simple types and for character arrays, strings and objects. Each method takes its second argument, converts it to a string and inserts the string into the StringBuilder in front of the character in the position specified by the first argument. The index specified by the first argument must be greater than or equal to 0 and less than the length of the StringBuilder; otherwise, the program throws an ArgumentOutOfRangeException.

Class StringBuilder also provides method Remove for deleting any portion of a StringBuilder. Method Remove takes two argumentsthe index at which to begin deletion and the number of characters to delete. The sum of the starting index and the number of characters to be deleted must always be less than the length of the StringBuilder; otherwise, the program throws an ArgumentOutOfRangeException. The Insert and Remove methods are demonstrated in Fig. 16.13.

Figure 16.13. StringBuilder text insertion and removal.

 1 // Fig. 16.13: StringBuilderInsertRemove.cs
 2 // Demonstrating methods Insert and Remove of the
 3 // StringBuilder class.
 4 using System;
 5 using System.Text;
 6
 7 class StringBuilderInsertRemove
 8 {
 9 public static void Main()
10 {
11 object objectValue = "hello";
12 string stringValue = "good bye";
13 char[] characterArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
14 bool booleanValue = true;
15 char characterValue = 'K';
16 int integerValue = 7;
17 long longValue = 10000000;
18 float floatValue = 2.5F; // F suffix indicates that 2.5 is a float
19 double doubleValue = 33.333;
20 StringBuilder buffer = new StringBuilder();
21
22 // insert values into buffer 
23 buffer.Insert( 0, objectValue ); 
24 buffer.Insert( 0, " " ); 
25 buffer.Insert( 0, stringValue ); 
26 buffer.Insert( 0, " " ); 
27 buffer.Insert( 0, characterArray );
28 buffer.Insert( 0, " " ); 
29 buffer.Insert( 0, booleanValue ); 
30 buffer.Insert( 0, " " ); 
31 buffer.Insert( 0, characterValue );
32 buffer.Insert( 0, " " ); 
33 buffer.Insert( 0, integerValue ); 
34 buffer.Insert( 0, " " ); 
35 buffer.Insert( 0, longValue ); 
36 buffer.Insert( 0, " " ); 
37 buffer.Insert( 0, floatValue ); 
38 buffer.Insert( 0, " " ); 
39 buffer.Insert( 0, doubleValue ); 
40 buffer.Insert( 0, " " ); 
41
42 Console.WriteLine( "buffer after Inserts: 
" + buffer + "
" );
43
44 buffer.Remove( 10, 1 ); // delete 2 in 2.5 
45 buffer.Remove( 4, 4 );  // delete .333 in 33.333
46
47 Console.WriteLine( "buffer after Removes:
" + buffer.ToString() );
48 } // end method Main
49 } // end class StringBuilderInsertRemove
 
buffer after Inserts:
 33.333 2.5 10000000 7 K True abcdef good bye hello
buffer after Removes:
 33 .5 10000000 7 K True abcdef good bye hello

Another useful method included with StringBuilder is Replace. Replace searches for a specified string or character and substitutes another string or character in its place. Figure 16.14 demonstrates this method.

Figure 16.14. StringBuilder text replacement.

 1 // Fig. 16.14: StringBuilderReplace.cs
 2 // Demonstrating method Replace.
 3 using System;
 4 using System.Text;
 5
 6 class StringBuilderReplace
 7 {
 8 public static void Main()
 9 {
10 StringBuilder builder1 =
11 new StringBuilder( "Happy Birthday Jane" );
12 StringBuilder builder2 =
13 new StringBuilder( "good bye greg" );
14
15 Console.WriteLine( "Before replacements:
" +
16 builder1.ToString() + "
" + builder2.ToString() );
17
18 builder1.Replace( "Jane", "Greg" );
19 builder2.Replace( 'g', 'G', 0, 5 );
20
21 Console.WriteLine( "
After replacements:
" +
22 builder1.ToString() + "
" + builder2.ToString() );
23 } // end method Main
24 } // end class StringBuilderReplace
 
Before Replacements:
Happy Birthday Jane
good bye greg

After replacements:
Happy Birthday Greg
Good bye greg

Line 18 uses method Replace to replace all instances of the string "Jane" with the string "Greg" in builder1. Another overload of this method takes two characters as parameters and replaces each occurrence of the first character with the second character. Line 19 uses an overload of Replace that takes four parameters, the first two of which are characters and the second two of which are ints. The method replaces all instances of the first character with the second character, beginning at the index specified by the first int and continuing for a count specified by the second int. Thus, in this case, Replace looks through only five characters, starting with the character at index 0. As the output illustrates, this version of Replace replaces g with G in the word "good", but not in "greg". This is because the gs in "greg" are not in the range indicated by the int arguments (i.e., between indexes 0 and 4).

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net