Document 15063146

advertisement
Mata kuliah : M0874 – Programming II
Tahun
: 2010
Strings, Characters, and Regular Expressions
Session 10
Outline Materi
•Introduction
•Fundamentals of Characters and Strings
•Strings
Bina Nusantara University
3
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.
Bina Nusantara University
4
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.
Bina Nusantara University
5
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”.
Bina Nusantara University
6
Strings Constructors
•Class string provides eight constructors for
initializing strings in various ways.
Bina Nusantara University
7
String indexer, Length Property
and 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.
Bina Nusantara University
8
Bina Nusantara University
9
Comparing Strings
Bina Nusantara University
10
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.
Bina Nusantara University
11
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.
Bina Nusantara University
12
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.
13
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
Bina Nusantara University
14
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.
Bina Nusantara University
15
Tips
Bina Nusantara University
16
References
•http://www.regular-expressions.info/dotnet.html
•http://en.csharponline.net/Manipulating_Strings_in_CSharp%E2%8
0%94Accessing_the_String%27s_Characters
Bina Nusantara University
17
Download