SlideShare a Scribd company logo
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.cs Encoder2.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 capacity StringBuilder 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 cont For 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. This is just the sum of squares of its components, which for mathematics buffs happens to be equal to the square 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 the specifier 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 Expressions System.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 of the * 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 like this 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 cont With regular expressions, you can perform quite  sophisticated and high- level operations on strings. For example, 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 Examples const 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

More Related Content

PPTX
Regular Expressions in PHP
PPT
testing add
PPTX
Basic data types in python
PPT
PHP Regular Expressions
PDF
Strings in Python
PPTX
An Introduction : Python
PPTX
Regular expressions in Python
PPSX
Programming with Python
Regular Expressions in PHP
testing add
Basic data types in python
PHP Regular Expressions
Strings in Python
An Introduction : Python
Regular expressions in Python
Programming with Python

What's hot (18)

PPT
101 3.7 search text files using regular expressions
PDF
Ruby quick ref
PPT
Adv. python regular expression by Rj
PDF
Ruby cheat sheet
PPT
Regular Expressions grep and egrep
PPT
3.7 search text files using regular expressions
PDF
Python regular expressions
PDF
Python revision tour II
PPT
Php basics
PPTX
Regex posix
PPT
Data type2 c
PPTX
Regular expressions and php
PPT
Enumerated data types in C
PDF
Python revision tour i
PPT
101 3.7 search text files using regular expressions
PPS
C programming session 08
PDF
Strings IN C
PPTX
Programming in C
101 3.7 search text files using regular expressions
Ruby quick ref
Adv. python regular expression by Rj
Ruby cheat sheet
Regular Expressions grep and egrep
3.7 search text files using regular expressions
Python regular expressions
Python revision tour II
Php basics
Regex posix
Data type2 c
Regular expressions and php
Enumerated data types in C
Python revision tour i
101 3.7 search text files using regular expressions
C programming session 08
Strings IN C
Programming in C
Ad

Similar to Csharp4 strings and_regular_expressions (20)

PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPT
Chapter 9 - Characters and Strings
DOCX
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
PPT
Java căn bản - Chapter9
PPT
2.regular expressions
PDF
Structures
PDF
Project lexical analyser compiler _1.pdf
PDF
Regular expressions
PPTX
C programming(part 3)
PDF
It’s sometimes useful to make a little language for a simple problem.pdf
PDF
C programming day#3.
PDF
Generating parsers using Ragel and Lemon
PPT
Getting started with c++
PPT
Getting started with c++
PPTX
Bsc cs i pic u-2 datatypes and variables in c language
PPTX
Btech i pic u-2 datatypes and variables in c language
PDF
C programming language
ODP
PHP Web Programming
PPTX
datatypes and variables in c language
PPTX
Lecture 7
Esoft Metro Campus - Certificate in c / c++ programming
Chapter 9 - Characters and Strings
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
Java căn bản - Chapter9
2.regular expressions
Structures
Project lexical analyser compiler _1.pdf
Regular expressions
C programming(part 3)
It’s sometimes useful to make a little language for a simple problem.pdf
C programming day#3.
Generating parsers using Ragel and Lemon
Getting started with c++
Getting started with c++
Bsc cs i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
C programming language
PHP Web Programming
datatypes and variables in c language
Lecture 7
Ad

More from Abed Bukhari (8)

PPT
Csharp4 arrays and_tuples
PPT
Csharp4 delegates lambda_and_events
PPT
Csharp4 generics
PPT
Csharp4 basics
PPT
Csharp4 operators and_casts
PPT
Csharp4 objects and_types
PPT
Csharp4 inheritance
PPT
Whats new in_csharp4
Csharp4 arrays and_tuples
Csharp4 delegates lambda_and_events
Csharp4 generics
Csharp4 basics
Csharp4 operators and_casts
Csharp4 objects and_types
Csharp4 inheritance
Whats new in_csharp4

Csharp4 strings and_regular_expressions

  • 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  • 2. Strings and Regular Expressions Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Building strings . Formatting expressions . Using regular expressions
  • 3. Examining System.String string message1 = &quot;Hello&quot;; // returns &quot;Hello&quot; message1 += &quot;, There&quot;; // returns &quot;Hello, There&quot; string message2 = message1 + &quot;!&quot;; // returns &quot;Hello, There!“ C# also allows extraction of a particular character using an indexer-like syntax: string message = &quot;Hello&quot;; char char4 = message[4]; // returns 'o'. Note the string is zero-indexed
  • 6. 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(&quot;Hello for all &quot;, 100 ); myBuilder Append(“students.&quot;);
  • 7. stringbuilder members StringBuilder sb = new StringBuilder(&quot;Hello&quot;); Or you can create an empty StringBuilder with a given capacity: StringBuilder sb = new StringBuilder(20); // another way to set the capacity StringBuilder sb = new StringBuilder(&quot;Hello&quot;); sb.Capacity = 100; a read-only MaxCapacity property : StringBuilder sb = new StringBuilder(100, 500 );
  • 9. Format Strings double d = 16.45; int i = 25; Console.WriteLine(&quot;The double is {0,10:E} and the int contains {1}&quot;, d, i);
  • 10. Format Strings cont For 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.
  • 11. How the string is formatted? Console.WriteLine(&quot;The double is {0,10:E} and the int contains {1}&quot;, 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})); }
  • 12. How the string is formatted?
  • 13. 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. This is just the sum of squares of its components, which for mathematics buffs happens to be equal to the square 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 the specifier 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) .
  • 14. Regular Expressions System.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 of the * 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 like this 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.
  • 15. Regular Expressions cont With regular expressions, you can perform quite sophisticated and high- level operations on strings. For example, 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)
  • 16. Regular Expressions Examples const string pattern = &quot;ion&quot;; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture ); foreach (Match nextMatch in myMatches) { Console.WriteLine(nextMatch.Index); } Another example in RegularExpressions.cs
  • 17. Regular Expressions cont const string pattern = @&quot;\bn&quot;; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture );
  • 19. 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
  • 20. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Editor's Notes

  • #6: //Encoder.cs using System; namespace Encoder { class Program { static void Main() { string greetingText = &amp;quot;Hello from all the students at Najah. &amp;quot;; greetingText += &amp;quot;We do hope you enjoy this codes as much as we enjoyed writing it&amp;quot;; for (int i = (int)&apos;z&apos;; i &gt;= (int)&apos;a&apos;; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } for (int i = (int)&apos;Z&apos;; i &gt;= (int)&apos;A&apos;; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } Console.WriteLine(&amp;quot;Encoded:\\n&amp;quot; + greetingText); Console.ReadLine(); } } } //Encoder2.cs using System; using System.Text; namespace Encoder2 { class Program { static void Main() { StringBuilder greetingBuilder = new StringBuilder(&amp;quot;Hello from all the students at Najah. &amp;quot;, 150); greetingBuilder.Append(&amp;quot;We do hope you enjoy this codes as much as we enjoyed writing it&amp;quot;); for(int i = (int)&apos;z&apos;; i&gt;=(int)&apos;a&apos; ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } for(int i = (int)&apos;Z&apos;; i&gt;=(int)&apos;A&apos; ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } Console.WriteLine(&amp;quot;Encoded:\\n&amp;quot; + greetingBuilder); Console.ReadLine(); } } }
  • #14: // FormattableVector.cs using System; using System.Text; namespace Najah.ILoveCsharp.FormattableVector { class MainEntryPoint { static void Main() { Vector v1 = new Vector(1,32,5); Vector v2 = new Vector(845.4, 54.3, -7.8); Console.WriteLine(&amp;quot;\\nIn IJK format,\\nv1 is {0,30:IJK}\\nv2 is {1,30:IJK}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nIn default format,\\nv1 is {0,30}\\nv2 is {1,30}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nIn VE format\\nv1 is {0,30:VE}\\nv2 is {1,30:VE}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nNorms are:\\nv1 is {0,20:N}\\nv2 is {1,20:N}&amp;quot;, v1, v2); } } struct Vector : IFormattable { public double x, y, z; public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(); string formatUpper = format.ToUpper(); switch (formatUpper) { case &amp;quot;N&amp;quot;: return &amp;quot;|| &amp;quot; + Norm() + &amp;quot; ||&amp;quot;; case &amp;quot;VE&amp;quot;: return String.Format(&amp;quot;( {0:E}, {1:E}, {2:E} )&amp;quot;, x, y, z); case &amp;quot;IJK&amp;quot;: StringBuilder sb = new StringBuilder(x.ToString(), 30); sb.Append(&amp;quot; i + &amp;quot;); sb.Append(y.ToString()); sb.Append(&amp;quot; j + &amp;quot;); sb.Append(z.ToString()); sb.Append(&amp;quot; k&amp;quot;); return sb.ToString(); default: return ToString(); } } public double Norm() { return x*x + y*y + z*z; } public Vector(Vector rhs) { x = rhs.x; y = rhs.y; z = rhs.z; } public override string ToString() { return &amp;quot;( &amp;quot; + x + &amp;quot; , &amp;quot; + y + &amp;quot; , &amp;quot; + z + &amp;quot; )&amp;quot;; } public double this [uint i] { get { switch (i) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException( &amp;quot;Attempt to retrieve Vector element&amp;quot; + i) ; } } set { switch (i) { case 0: x = value; break; case 1: y = value; break; case 2: z = value; break; default: throw new IndexOutOfRangeException( &amp;quot;Attempt to set Vector element&amp;quot; + i) ; } } } //public static bool operator == (Vector lhs, Vector rhs) //{ // if (lhs.x == rhs.x &amp;&amp; lhs.y == rhs.y &amp;&amp; lhs.z == rhs.z) // return true; // return false; //} private const double Epsilon = 0.0000001; public static bool operator == (Vector lhs, Vector rhs) { if (Math.Abs(lhs.x - rhs.x) &lt; Epsilon &amp;&amp; Math.Abs(lhs.y - rhs.y) &lt; Epsilon &amp;&amp; Math.Abs(lhs.z - rhs.z) &lt; Epsilon ) return true; return false; } public static bool operator != (Vector lhs, Vector rhs) { return ! (lhs == rhs); } public static Vector operator + (Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; } public static Vector operator * (double lhs, Vector rhs) { return new Vector(lhs*rhs.x, lhs*rhs.y, lhs*rhs.z); } public static Vector operator * (Vector lhs, double rhs) { return rhs*lhs; } public static double operator * (Vector lhs, Vector rhs) { return lhs.x*rhs.x + lhs.y+rhs.y + lhs.z*rhs.z; } } } /* Results: FormattableVector In IJK format, v1 is 1 i + 32 j + 5 k v2 is 845.4 i + 54.3 j + -7.8 k In default format, v1 is ( 1, 32, 5 ) v2 is ( 845.4, 54.3, -7.8 ) In VE format v1 is ( 1.000000E+000, 3.200000E+001, 5.000000E+000 ) v2 is ( 8.454000E+002, 5.430000E+001, -7.800000E+000 ) Norms are: v1 is || 1050 || v2 is || 717710.49 || */
  • #17: using System; using System.Text.RegularExpressions; namespace Najah.ILoveCsharp.RegularExpressionPlayaround { class MainEntryPoint { static void Main() { Find1(); Console.ReadLine(); } static void Find1() { const string text = @&amp;quot;XML has made a major impact in almost every aspect of software development. Designed as an open, extensible, self-describing language, it has become the standard for data and document delivery on the web. The panoply of XML-related technologies continues to develop at breakneck speed, to enable validation, navigation, transformation, linking, querying, description, and messaging of data.&amp;quot;; const string pattern = @&amp;quot;\\bn\\S*ion\\b&amp;quot;; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); WriteMatches(text, matches); } static void Find2() { const string text = @&amp;quot;XML has made a major impact in almost every aspect of software development. Designed as an open, extensible, self-describing language, it has become the standard for data and document delivery on the web. The panoply of XML-related technologies continues to develop at breakneck speed, to enable validation, navigation, transformation, linking, querying, description, and messaging of data.&amp;quot;; const string pattern = @&amp;quot;\\bn&amp;quot;; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); WriteMatches(text, matches); } static void WriteMatches(string text, MatchCollection matches) { Console.WriteLine(&amp;quot;Original text was: \\n\\n&amp;quot; + text + &amp;quot;\\n&amp;quot;); Console.WriteLine(&amp;quot;No. of matches: &amp;quot; + matches.Count); foreach (Match nextMatch in matches) { int index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (index &lt; 5) ? index : 5; int fromEnd = text.Length - index - result.Length; int charsAfter = (fromEnd &lt; 5) ? fromEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; Console.WriteLine(&amp;quot;Index: {0}, \\tString: {1}, \\t{2}&amp;quot;, index, result, text.Substring(index - charsBefore, charsToDisplay)); } } } }