17

Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Embed Size (px)

DESCRIPTION

Bina Nusantara University 3 Outline Materi Introduction Fundamentals of Characters and Strings Strings

Citation preview

Page 1: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010
Page 2: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Strings, Characters, and Regular ExpressionsSession 10

Mata kuliah : M0874 – Programming IITahun : 2010

Page 3: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

3

Outline Materi•Introduction•Fundamentals of Characters and Strings•Strings

Page 4: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

4

Introduction•This chapter introduces the .NET Framework Class Library’s string- and character-processing capabilities and demonstrates how to use regular expressions to search for patterns in text.

Page 5: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

5

Fundamentals of Characters and Strings•Characters are the fundamental building blocks of C# source code.•Every program is composed of characters that, when grouped together meaningfully, create a sequence that the compiler interprets as instructions describing how to accomplish a task.•A string is a series of characters treated as a unit.•These characters can be uppercase letters, lowercase letters, digits and various special characters: +, -, *, /, $ and others.

Page 6: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

6

Fundamentals of Characters and Strings•Example (sequences of charcaters in double quotation marks):

“John Q. Doe”“9999 Main Street”“Waltham, Massachusetts”“(201) 555-1212”

•A declaration can assign a string literal to a string reference.

string color = “blue”;•Initialize string reference color to refer to the string literal object “blue”.

Page 7: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

7

Strings Constructors•Class string provides eight constructors for initializing strings in various ways.

Page 8: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

8

String indexer, Length Propertyand CopyTo Method

•String indexer facilitates the retrieval of any character in the string.•String property length returns the length of the string.•String method CopyTo copies a specified number of characters from a string into a char array.

Page 9: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

9

Page 10: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

10

Comparing Strings

Page 11: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

11

Extracting Substrings from strings•Class string provides two Substring methods, which create a new string by copying part of an existing string.•Each method returns a new string.

Page 12: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

12

Finding a Substring within a String•To find the first or last occurrence of a character within the string:

•Using a string variable type int index = str.IndexOf(@"\"); where index is a variable that will store the zero-based position of the character within the string, str is the variable you want to search, and @"\" is the string you are searching for. •Type int index=str.LastIndexOf(@"\"); to search for the last occurrence of a substring within the string.

Page 13: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

13

Tips•There are two ways of enumerating through all the characters in a string.

•One way is to write a loop where you maintain an index with an integer variable. The index begins at zero and increases until the index is equal to Length –1 (see "Finding the String's Length" earlier in this chapter) (Figure 4.41). •The other way is to use a function called foreach.

Page 14: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

14

Tips•If you ask for an index greater than Length –1, the string class generates an exception. The exception (or error) is IndexOutOfBoundsException. •You can access the characters of the string, but you can't modify them since strings are immutable

Page 15: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

15

Tips•If the IndexOf or LastIndexOf functions don't find any occurrences of the substring, the functions return –1. •There are a few variations of the IndexOf and LastIndexOf functions. One variation lets you specify a location within the string where you want to start the search. This is useful for cases in which you wish to scan the string for one character, then when you find it, you want to get the next occurrence of the same character. In that case you specify that you want to begin the search in the position after the first occurrence.

Page 16: Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University

16

Tips