Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010."— Presentation transcript:

1

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

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

4 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.

5 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.

6 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”.

7 Bina Nusantara University 7 Strings Constructors Class string provides eight constructors for initializing strings in various ways.

8 Bina Nusantara University 8 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.

9 Bina Nusantara University 9

10 10 Comparing Strings

11 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.

12 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.

13 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.

14 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

15 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.

16 Bina Nusantara University 16 Tips

17 Bina Nusantara University 17 References http://www.regular-expressions.info/dotnet.html http://en.csharp- online.net/Manipulating_Strings_in_CSharp%E2%8 0%94Accessing_the_String%27s_Charactershttp://en.csharp- online.net/Manipulating_Strings_in_CSharp%E2%8 0%94Accessing_the_String%27s_Characters


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

Similar presentations


Ads by Google