Strings, Characters and Regular Expressions

1 2 OBJECTIVES 16 In this chapter you will learn: ƒ To create and manipulate immutable character string objects of class string. Strings, Charact...
Author: Vernon Clarke
0 downloads 1 Views 152KB Size
1

2

OBJECTIVES

16

In this chapter you will learn: ƒ To create and manipulate immutable character string objects of class string.

Strings, Characters Strings and Regular Expressions © 2006 Pearson Education, Inc. All rights reserved.

3

16.3 string Constructors •string Constructors – Can initialize string as if it was a primitive type Ex: string example = “I see…”;

– Can initialize string in the same way as a normal class (Eight constructors) Ex: string example = new string( “I see…” );

© 2006 Pearson Education, Inc. All rights reserved.

1

// Fig. 16.1: StringConstructor.cs

2 3 4 5

// Demonstrating string class constructors. using System;

6 7 8 9

{

class StringConstructor public static void Main() { string originalString, string1, string2,

10 11 12 13

string3, string4; char[] characterArray = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };

14 15 16 17

// string initialization originalString = "Welcome to C# programming!"; string1 = originalString; string2 = new string( characterArray );

18 19

string3 = new string( characterArray, 6, 3 ); string4 = new string( 'C', 5 );

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

20

5

Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n"Outline + "string2 = " + "\"" + string2 + "\"\n" +

21 22

"string3 = " + "\"" + string3 + "\"\n" +

23 24 25

4

"string4 = " + "\"" + string4 + "\"\n" ); } // end method Main

26 } // end class StringConstructor

StringConstructor .cs

(2 of 2) string1 string2 string3 string4

= = = =

6

16.4 string Indexer, Length Property and CopyTo Method •string indexer – Facilitates the retrieval of any character in the string – Treats a string as an array of chars

"Welcome to C# programming!" "birth day" "day" "CCCCC"

• Return the character at the specific position in the string

•Length h property – Returns the length of the string

•CopyTo Method – Copies a specified number of characters into a char array

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

1

1 2 3

// Fig. 16.2: StringMethods.cs // Using the indexer, property Length and method CopyTo // of class string.

4 5 6

using System;

7 8

{

9 10

7

Outline

Console.Write( "\nThe character array is: " ); for ( int i = 0; i < characterArray.Length; i++ ) Console.Write( characterArray[ i ] );

33 34

Console.WriteLine( "\n" );

35

string string1;

characterArray = new char[ 5 ];

string1.CopyTo( 0, characterArray, 0, characterArray.Length );

32

{

14 15 16

// copy characters from string1 into characterArray

29 31

(1 of 2)

public static void Main()

char[] characterArray;

8

28 30

StringMethods.cs

class StringMethods

11 12 13

27

} // end method Main

36

37 } // end class StringMethods

string1 = "hello there";

string1: "hello there" Length of string1: 11 The string reversed is: ereht olleh The character array is: hello

// output string1

17 18 19 20 21

Console.WriteLine( "string1: \"" + string1 + "\"" );

22 23 24

// loop through characters in string1 and display reversed Console.Write( "The string reversed is: " );

25 26

for ( int i = string1.Length - 1; i >= 0; i-- ) Console.Write( string1[ i ] );

// test Length property Console.WriteLine( "Length of string1: " + string1.Length );

© 2006 Pearson Education, Inc. All rights reserved.

9

16.5 Comparing strings

© 2006 Pearson Education, Inc. All rights reserved.

1 2 3 4 5

// Fig. 16.3: StringCompare.cs // Comparing strings using System; class StringCompare

6 { 7 8 9 10 11

• Comparing String objects – Method Equals or == • Determine if the strings are the same • Returns bool value

– Method CompareTo

– Method StartsWith • Determines if string instance starts with the string text passed to it as an argument

– Method EndWith • Determines if string instance ends with the string text passed to it as an argument

StringCompare.cs

public static void Main() { string string1 = "hello"; string string2 = "good bye"; string string3 = "Happy Birthday";

// output values of four strings Console.WriteLine( "string1 = \"" + string1 + "\"" + "\nstring2 = \"" + string2 + "\"" + "\nstring3 = \"" + string3 + "\"" + "\nstring4 = \"" + string4 + "\"\n" ); // test for equality using Equals method if ( string1.Equals( "hello" ) ) Console.WriteLine( "string1 equals \"hello\"" ); else Console.WriteLine( "string1 does not equal \"hello\"" ); © 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

25 26

// test for equality with ==

27 28

if ( string1 == "hello" ) Console.WriteLine( "string1 equals \"hello\"" );

29

else

11

// test for equality comparing case

33 34

if ( string.Equals( string3, string4 ) ) // static method Console.WriteLine( "string3 equals string4" );

35 36 37

12

"hello" "good bye" "Happy Birthday" "happy birthday"

string1.CompareTo( string2.CompareTo( string1.CompareTo( string3.CompareTo( string4.CompareTo(

else

// test CompareTo

39 40

Console.WriteLine ( "\nstring1.CompareTo( string2 ) is " + string1.CompareTo( string2 ) + "\n" +

41

"string2.CompareTo( string1 ) is " +

42 43 44

string2.CompareTo( string1 ) + "\n" + "string1.CompareTo( string1 ) is " + string1.CompareTo( string1 ) + "\n" +

45 46

"string3.CompareTo( string4 ) is " + string3.CompareTo( string4 ) + "\n" +

48 49

= = = =

Outline

string2 string1 string1 string4 string3

) ) ) ) )

is is is is is

StringCompare.cs 1 -1 0 1 -1

(3 of 3)

Console.WriteLine( "string3 does not equal string4" );

38

47

string1 string2 string3 string4

string1 equals "hello" string1 equals "hello" string3 does not equal string4

Console.WriteLine( "string1 does not equal \"hello\"" );

30 31 32

(1 of 3)

string string4 = "happy birthday";

12 13 14 15 16 17 18 19 20 21 22 23 24

• Returns 0 if strings g are equal q • Returns negative value if the string invoked is less than the string that is passed in • Returns positive value if the string invoked is greater than the string that is passed in

10

Outline

"string4.CompareTo( string3 ) is " + string4.CompareTo( string3 ) + "\n\n" ); } // end method Main

50 } // end class StringCompare

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

2

1

// Fig. 16.4: StringStartEnd.cs

2 3

// Demonstrating StartsWith and EndsWith methods. using System;

Outline

class StringStartEnd {

StringStartEnd.cs

13

4 5 6 7 8 9 10

(1 of 2)

public static void Main() { string[] strings = { "started", "starting", "ended", "ending" };

11 12

// test every string to see if it starts with "st"

13

f for ( int i t i = 0; 0 i < strings.Length; t i L th i++ i )

14 15 16 17 18

19 20 // test every string to see if it ends with "ed" 21 for ( int i = 0; i < strings.Length; i++ ) 22 if ( strings[ i ].EndsWith( "ed" ) ) 23 Console.WriteLine( "\"" + strings[ i ] + "\"" + 24 " ends with \"ed\"" ); 25 26 Console.WriteLine( "" ); 27 } // end method Main 28 } // end class StringStartEnd

14

Outline

StringStartEnd.cs

(2 of 2)

"started" starts with "st" "starting" starts with "st" "started" ends with "ed" "ended" ends with "ed"

if ( strings[ i ].StartsWith( "st" ) ) Console.WriteLine( "\"" + strings[ i ] + "\"" + " starts with \"st\"" ); Console.WriteLine( "" );

© 2006 Pearson Education, Inc. All rights reserved.

15

16.6 Locating Characters and Substrings in strings • Search for characters in string

© 2006 Pearson Education, Inc. All rights reserved.

1

// Fig. 16.5: StringIndexMethods.cs

2 3

// Using string searching methods. using System;

4 5

class StringIndexMethods

6

{

7 8

– Method IndexOf

9 10

• Returns the index of first occurrence of a character or substring; -1 if not found

public static void Main() { string letters = "abcdefghijklmabcdefghijklm"; char[] searchLetters = { 'c', 'a', '$' };

11

– Method IndexOfAny • Same as IndexOf excepts it takes in an array of characters and returns the index of the first occurrence of any of the characters in the array

– Method LastIndexOf • Returns the index of last occurrence of a character or substring; -1 if not found

12 13

// test IndexOf to locate a character in a string Console.WriteLine( "First 'c' is located at index " +

14 15

letters IndexOf( 'c' letters.IndexOf( c ) ); Console.WriteLine( "First 'a' starting at 1 is located at index " +

16 17

letters.IndexOf( 'a', 1 ) ); Console.WriteLine( "First '$' in the 5 positions starting at 3 " +

18

– Method LastIndexOfAny • Same as LastIndexOf excepts it takes in an array of characters and returns the index of the last occurrence of any of the characters in the array

"is located at index " + letters.IndexOf( '$', 3, 5 ) );

19 20

// test LastIndexOf to find a character in a string

21 22

Console.WriteLine( "\nLast 'c' is located at index " + letters.LastIndexOf( 'c' ) );

23

Console.WriteLine( "Last 'a' up to position 25 is located at " +

24 25

"index " + letters.LastIndexOf( 'a', 25 ) ); Console.WriteLine( "Last '$' in the 5 positions starting at 15 " +

26

"is located at index " + letters.LastIndexOf( '$', 15, 5 )©); 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

27

16

17

54

18

28

// test IndexOf to locate a substring in a string

55

// test LastIndexOfAny to find last occurrence of character

29

Console.WriteLine( "\nFirst \"def\" is located at index " +

30 31

letters.IndexOf( "def" ) ); Console.WriteLine( "First \"def\" starting at 7 is located at " +

56 57

// in array Console.WriteLine( "\nLast 'c', 'a' or '$' is " +

32 33 34 35 36

"index " + letters.IndexOf( "def", 7 ) ); Console.WriteLine( "First \"hello\" in the 15 positions " + "starting at 5 is located at index " + letters.IndexOf( "hello", 5, 15 ) );

58

"located at index " + letters.LastIndexOfAny( searchLetters ) );

59

Console.WriteLine( "Last 'c', 'a' or '$' up to position 1 is " +

60 61 62

"located at index " + letters.LastIndexOfAny( searchLetters, 1 ) ); Console.WriteLine( "Last 'c', 'a' or '$' in the 5 positions " +

37

// test LastIndexOf to find a substring in a string

63

"ending at 25 is located at index " +

38

Console.WriteLine( "\nLast \"def\" is located at index " +

64

letters.LastIndexOfAny( searchLetters, 25, 5 ) );

39

letters.LastIndexOf( "def" ) );

40 41

Console.WriteLine( "Last \"def\" up to position 25 is located " + "at index " + letters.LastIndexOf( "def", 25 ) );

42

Console.WriteLine( "Last \"hello\" in the 15 positions " +

43

"ending at 20 is located at index " + letters.LastIndexOf( "hello", 20, 15 ) );

44 45 46

// test IndexOfAny to find first occurrence of character in array

47

Console.WriteLine( "\nFirst 'c', 'a' or '$' is " +

48

65 } // end method Main 66 } // end class StringIndexMethods

"located at index " + letters.IndexOfAny( searchLetters ) );

49

Console.WriteLine( "First 'c', 'a' or '$' starting at 7 is " +

50 51

"located at index " + letters.IndexOfAny( searchLetters, 7 ) ); Console.WriteLine( "First 'c', 'a' or '$' in the 5 positions " +

52

"starting at 7 is located at index " +

53

letters.IndexOfAny( searchLetters, 7, 5 ) );

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

3

First 'c' is located at index 2 First 'a' starting at 1 is located at index 13 First '$' in the 5 positions starting at 3 is located at index -1

19

16.7 Extracting Substrings from strings

Last 'c' is located at index 15 Last 'a' up to position 25 is located at index 13 Last '$' in the 5 positions starting at 15 is located at index -1 First "def" is located at index 3 First "def" starting at 7 is located at index 16 First "hello" in the 15 positions starting at 5 is located at index -1 Last "def" is located at index 16 Last "def" up to position 25 is located at index 16 Last "hello" in the 15 positions ending at 20 is located at index -1

20

Outline

StringIndexMethods .cs

(4 of 4)

• Method Substring – Creates and returns a new string by copying part of an existing string

First 'c', 'a' or '$' is located at index 0 First 'c', 'a' or '$' starting at 7 is located at index 13 First 'c', 'a' or '$' in the 5 positions starting at 7 is located at index -1 Last 'c', 'a' or '$' is located at index 15 Last 'c', 'a' or '$' up to position 1 is located at index 0 Last 'c', , 'a' or '$' in the 5 positions p ending g at 25 is located at index -1

1

// Fig. 16.6: SubString.cs

2

// Demonstrating the string Substring method.

3

using System;

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

21

22

Outline

16.8 Concatenating Strings

4 5

class SubString

6

{

7

public static void Main()

8

{

SubString.cs

• Method Concat or + – Returns a new string containing the combined characters from both original strings

string letters = "abcdefghijklmabcdefghijklm";

9 10 11

// invoke Substring method and pass it one parameter

12

Console.WriteLine( "Substring from index 20 to end is \"" + letters.Substring( ette s Subst g( 20 0 ) + "\"" \ );

13 3 14 15

// invoke Substring method and pass it two parameters

16

Console.WriteLine( "Substring from index 0 of length 6 is \"" + letters.Substring( 0, 6 ) + "\"" );

17

} // end method Main

18

19 } // end class SubString Substring from index 20 to end is "hijklm" Substring from index 0 of length 6 is "abcdef"

1

// Fig. 16.7: SubConcatenation.cs

2

// Demonstrating string class Concat method.

3

using System;

4 5

class StringConcatenation

6

{

7

public static void Main()

8

{

9

string string1 = "Happy ";

10 11

string string2 = "Birthday";

12

Console.WriteLine( "string1 = \"" + string1 + "\"\n" +

13 14 15 16 17 18

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

23

24

Outline

16.9 Miscellaneous string Methods SubConcatenation .cs

• Miscellaneous string methods – Method Replace • Returns a new string replacing every occurrence of the specified phrase with another phrase in the string

"string2 = \"" + string2 + "\"" );

– Method ToLower

Console.WriteLine(

• Returns a new lower cased version of the string

"\nResult of string.Concat( string1, string2 ) = " + string.Concat( string1, string2 ) ); Console.WriteLine( "string1 after concatenation = " + string1 ); } // end method Main

19 } // end class StringConcatenation

– Method ToUpper • Returns a new upper cased version of the string

– Method Trim • Remove all white space characters from the string

string1 = "Happy " string2 = "Birthday" Result of string.Concat( string1, string2 ) = Happy Birthday string1 after concatenation = Happy © 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

4

1

// Fig. 16.8: StringMethods2.cs

2 3

// Demonstrating string methods Replace, ToLower, ToUpper, Trim, // and ToString.

4

using System;

5 6

class StringMethods2

7 8 9

{

string string1 = "cheers!";

11 12

string string2 = "GOOD BYE "; string string3 = " spaces ";

"string2 = \"" + string2 + "\"\n" + "string3 = \"" + string3 + "\"" );

30 31 32

Console.WriteLine( "\nstring1 = \"" + string1 + "\"" );

26

Outline

string3.Trim() + "\"" );

} // end method Main

string1 = "cheers!" string2 = "GOOD BYE " string3 = " spaces

StringMethods2.cs

(2 of 2)

"

string1.ToUpper() = "CHEERS!" string2.ToLower() = "good bye " string3 after trim = "spaces"

// call method Replace Console.WriteLine( "\nReplacing \"e\" with \"E\" in string1: \"" +

string1 = "cheers!"

string1.Replace( 'e', 'E' ) + "\"" );

22 23

// call ToLower and ToUpper

24 25

Console.WriteLine( "\nstring1.ToUpper() = \"" + string1.ToUpper() + "\"\nstring2.ToLower() = \"" +

26

Console.WriteLine( "\nstring3 after trim = \"" +

34 } // end class StringMethods2

Console WriteLine( "string1 Console.WriteLine( string1 = \ \"" + string1 + "\"\n" \ \n +

16 17

21

// call Trim method

29

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

15

18 19 20

27 28

33

public static void Main() {

10

13 14

25

string2.ToLower() + "\"" );

© 2006 Pearson Education, Inc. All rights reserved.

© 2006 Pearson Education, Inc. All rights reserved.

5