Miscellaneous string Methods

Class string provides several methods that return modified copies of strings. The application in Fig. 16.8 demonstrates the use of these methods, which include string methods Replace, ToLower, ToUpper and trim.

Figure 16.8. string methods Replace, ToLower, ToUpper and TRim.

 1 // Fig. 16.8: StringMethods2.cs
 2 // Demonstrating string methods Replace, ToLower, ToUpper, Trim,
 3 // and ToString.
 4 using System;
 5
 6 class StringMethods2
 7 {
 8 public static void Main()
 9 {
10 string string1 = "cheers!";
11 string string2 = "GOOD BYE ";
12 string string3 = " spaces ";
13
14 Console.WriteLine( "string1 = "" + string1 + ""
" +
15 "string2 = "" + string2 + ""
" +
16 "string3 = "" + string3 + """ );
17
18 // call method Replace
19 Console.WriteLine(
20 "
Replacing "e" with "E" in string1: "" +
21 string1.Replace( 'e', 'E' ) + """ );
22
23 // call ToLower and ToUpper 24 Console.WriteLine( " string1.ToUpper() = "" + 25 string1.ToUpper() + "" string2.ToLower() = "" + 26 string2.ToLower() + """ ); 27 28 // call Trim method 29 Console.WriteLine( " string3 after trim = "" + 30 string3.Trim() + """ ); 31 32 Console.WriteLine( " string1 = "" + string1 + """ ); 33 } // end method Main 34 } // end class StringMethods2
string1 = "cheers!"
string2 = "GOOD BYE "
string3 = " spaces "

Replacing "e" with "E" in string1: "chEErs!"

string1.ToUpper() = "CHEERS!"
string2.ToLower() = "good bye "

string3 after trim = "spaces"

string1 = "cheers!"

Line 21 uses string method Replace to return a new string, replacing every occurrence in string1 of character 'e' with 'E'. Method Replace takes two argumentsa string for which to search and another string with which to replace all matching occurrences of the first argument. The original string remains unchanged. If there are no occurrences of the first argument in the string, the method returns the original string.

string method ToUpper generates a new string (line 25) that replaces any lowercase letters in string1 with their uppercase equivalents. The method returns a new string containing the converted string; the original string remains unchanged. If there are no characters to convert, the original string is returned. Line 26 uses string method ToLower to return a new string in which any uppercase letters in string2 are replaced by their lowercase equivalents. The original string is unchanged. As with ToUpper, if there are no characters to convert to lowercase, method ToLower returns the original string.

Line 30 uses string method trim to remove all whitespace characters that appear at the beginning and end of a string. Without otherwise altering the original string, the method returns a new string that contains the string, but omits leading or trailing whitespace characters. Another version of method trim takes a character array and returns a string that does not contain the characters in the array argument.

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