csharp4 strings and_regular_expressions

Post on 06-May-2015

822 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Abed El-Azeem Bukhari (MCPD,MCTS and MCP)el-bukhari.com

Strings and Regular Expressions

Prepared By : Abed ElAzeem Bukhari

What’s in This Chapter?. Building strings. Formatting expressions. Using regular expressions

Examining System.String

string message1 = "Hello"; // returns "Hello"message1 += ", There"; // returns "Hello, There"string message2 = message1 + "!"; // returns "Hello, There!“

C# also allows extraction of a particular character using an indexer-like syntax:

string message = "Hello";char char4 = message[4]; // returns 'o'. Note the string is zero-indexed

System.String methods

building strings

Encoder.csEncoder2.cs

building strings cont.

The StringBuilder class has two main properties: ➤ Length , which indicates the length of the string that it actually contains ➤ Capacity , which indicates the maximum length of the string in the memory

allocation

StringBuilder myBuilder = new StringBuilder("Hello for all ", 100); myBuilder Append(“students.");

stringbuilder members

StringBuilder sb = new StringBuilder("Hello");

Or you can create an empty StringBuilder with a given capacity:

StringBuilder sb = new StringBuilder(20);// another way to set the capacityStringBuilder sb = new StringBuilder("Hello");sb.Capacity = 100;

a read-only MaxCapacity property :StringBuilder sb = new StringBuilder(100, 500);

stringbuilder members cont

Format Strings

double d = 16.45;int i = 25;Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i);

Format Strings contFor example, you can include:

-The number of characters to be occupied by the representation of the item, prefixed by a comma. A negative number indicates that the item should be left-justified, whereas a positive number indicates that it should be right-justified. If the item actually occupies more characters than have been requested, it will still appear in full.

- A format specifier, preceded by a colon. This indicates how you want the item to be formatted. For example, you can indicate whether you want a number to be formatted as a currency or displayed in scientific notation.

How the string is formatted?

Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i);

// Likely implementation of Console.WriteLine()public void WriteLine(string format, object arg0, object arg1){this.WriteLine(string.Format(this.FormatProvider, format,new object[]{arg0, arg1}));}

How the string is formatted?

The formattableVector example

FormattableVector.cs

The format specifiers you are going to support are:- N — Should be interpreted as a request to supply a quantity known as the Norm of the Vector. Thisis just the sum of squares of its components, which for mathematics buffs happens to be equal to thesquare of the length of the Vector, and is usually displayed between double vertical bars, like this:||34.5||.- VE — Should be interpreted as a request to display each component in scientific format, just as thespecifier E applied to a double indicates (2.3E+01, 4.5E+02, 1.0E+00).- IJK — Should be interpreted as a request to display the vector in the form 23i + 450j + 1k.-Anything else should simply return the default representation of the Vector (23, 450, 1.0).

Regular ExpressionsSystem.Text.RegularExpressions

The regular expressions language is designed specifically for string processing. It contains two features:

➤ A set of escape codes for identifying specific types of characters. You will be familiar with the use ofthe * character to represent any substring in DOS expressions. (For example, the DOS command Dir Re* lists the files with names beginning with Re .) Regular expressions use many sequences likethis to represent items such as any one character , a word break , one optional character , and so on.

➤ A system for grouping parts of substrings and intermediate results during a search operation.

Regular Expressions contWith regular expressions, you can perform quite sophisticated and high- level operations on strings. Forexample, you can:

➤ Identify (and perhaps either flag or remove) all repeated words in a string (for example, “T he computer books books ” to “ The computer books ” )

➤ Convert all words to title case (for example, “ this is a Title ” to “ This Is A Title ” )

➤ Convert all words longer than three characters to title case (for example, “ this is a Title ” to “ This is a Title ” )

➤ Ensure that sentences are properly capitalized ➤ Separate the various elements of a URI (for example, given

http://www.najahclub.net , extract the protocol, computer name, file name, and so on)

Regular Expressions Examplesconst string pattern = "ion";

MatchCollection myMatches = Regex.Matches(myText, pattern,RegexOptions.IgnoreCase |RegexOptions.ExplicitCapture);

foreach (Match nextMatch in myMatches){Console.WriteLine(nextMatch.Index);}

Another example in RegularExpressions.cs

Regular Expressions cont

const string pattern = @"\bn";MatchCollection myMatches = Regex.Matches(myText, pattern,RegexOptions.IgnoreCase |RegexOptions.ExplicitCapture);

Regular Expressions patterns

Regular Expressions Matches, Groups, and Captures

For example, URIs have the format <protocol> ://<address> :<port> , where the port is optional. An example of this is http://www.el-bukhari.com:4355 .

Suppose that you want to extract the protocol, the address, and the port from a URI, where you know that there may or may not be whitespace (but no punctuation)immediately following the URI. You could do so using this expression:

\b(\S+)://([^:]+)(?::(\S+))?\b

Thanks For Attending

Abed El-Azeem Bukhari (MCPD,MCTS and MCP)

el-bukhari.com

top related