string Indexer, Length Property and CopyTo Method

The application in Fig. 16.2 presents the string indexer, which facilitates the retrieval of any character in the string, and the string property Length, which returns the length of the string. The string method CopyTo copies a specified number of characters from a string into a char array.

Figure 16.2. string indexer, Length property and CopyTo method.

 1 // Fig. 16.2: StringMethods.cs
 2 // Using the indexer, property Length and method CopyTo
 3 // of class string.
 4 using System;
 5
 6 class StringMethods
 7 {
 8 public static void Main()
 9 {
10 string string1;
11 char[] characterArray;
12
13 string1 = "hello there";
14 characterArray = new char[ 5 ];
15
16 // output string1 17 Console.WriteLine( "string1: "" + string1 + """ ); 18 19 // test Length property 20 Console.WriteLine( "Length of string1: " + string1.Length ); 21 22 // loop through characters in string1 and display reversed 23 Console.Write( "The string reversed is: " ); 24 25 for ( int i = string1.Length - 1; i >= 0; i-- ) 26 Console.Write( string1[ i ] ); 27 28 // copy characters from string1 into characterArray 29 string1.CopyTo( 0, characterArray, 0, characterArray.Length ); 30 Console.Write( " The character array is: " ); 31 32 for ( int i = 0; i < characterArray.Length; i++ ) 33 Console.Write( characterArray[ i ] ); 34 35 Console.WriteLine( " " ); 36 } // end method Main 37 } // end class StringMethods
string1: "hello there"
Length of string1: 11
The string reversed is: ereht olleh
The character array is: hello

This application determines the length of a string, displays its characters in reverse order and copies a series of characters from the string to a character array.

Line 20 uses string property Length to determine the number of characters in string1. Like arrays, strings always know their own size.

Lines 2526 write the characters of string1 in reverse order using the string indexer. The string indexer treats a string as an array of chars and returns the character at a specific position in the string. The indexer receives an integer argument as the position number and returns the character at that position. As with arrays, the first element of a string is considered to be at position 0.

Common Programming Error 16 1

Attempting to access a character that is outside a string's bounds (i.e., an index less than 0 or an index greater than or equal to the string's length) results in an IndexOutOfRangeException.

Line 29 uses string method CopyTo to copy the characters of string1 into a character array (characterArray). The first argument given to method CopyTo is the index from which the method begins copying characters in the string. The second argument is the character array into which the characters are copied. The third argument is the index specifying the starting location at which the method begins placing the copied characters into the character array. The last argument is the number of characters that the method will copy from the string. Lines 3233 output the char array contents one character at a time.

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