Csharp4 strings and_regular_expressions

931 views
879 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
931
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
91
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • //Encoder.cs using System; namespace Encoder { class Program { static void Main() { string greetingText = "Hello from all the students at Najah. "; greetingText += "We do hope you enjoy this codes as much as we enjoyed writing it"; for (int i = (int)'z'; i >= (int)'a'; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } for (int i = (int)'Z'; i >= (int)'A'; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } Console.WriteLine("Encoded:\\n" + greetingText); Console.ReadLine(); } } } //Encoder2.cs using System; using System.Text; namespace Encoder2 { class Program { static void Main() { StringBuilder greetingBuilder = new StringBuilder("Hello from all the students at Najah. ", 150); greetingBuilder.Append("We do hope you enjoy this codes as much as we enjoyed writing it"); for(int i = (int)'z'; i>=(int)'a' ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } for(int i = (int)'Z'; i>=(int)'A' ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } Console.WriteLine("Encoded:\\n" + greetingBuilder); Console.ReadLine(); } } }
  • // 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("\\nIn IJK format,\\nv1 is {0,30:IJK}\\nv2 is {1,30:IJK}", v1, v2); Console.WriteLine("\\nIn default format,\\nv1 is {0,30}\\nv2 is {1,30}", v1, v2); Console.WriteLine("\\nIn VE format\\nv1 is {0,30:VE}\\nv2 is {1,30:VE}", v1, v2); Console.WriteLine("\\nNorms are:\\nv1 is {0,20:N}\\nv2 is {1,20:N}", 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 "N": return "|| " + Norm() + " ||"; case "VE": return String.Format("( {0:E}, {1:E}, {2:E} )", x, y, z); case "IJK": StringBuilder sb = new StringBuilder(x.ToString(), 30); sb.Append(" i + "); sb.Append(y.ToString()); sb.Append(" j + "); sb.Append(z.ToString()); sb.Append(" k"); 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 "( " + x + " , " + y + " , " + z + " )"; } public double this [uint i] { get { switch (i) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException( "Attempt to retrieve Vector element" + i) ; } } set { switch (i) { case 0: x = value; break; case 1: y = value; break; case 2: z = value; break; default: throw new IndexOutOfRangeException( "Attempt to set Vector element" + i) ; } } } //public static bool operator == (Vector lhs, Vector rhs) //{ // if (lhs.x == rhs.x && lhs.y == rhs.y && 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) < Epsilon && Math.Abs(lhs.y - rhs.y) < Epsilon && Math.Abs(lhs.z - rhs.z) < 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 || */
  • using System; using System.Text.RegularExpressions; namespace Najah.ILoveCsharp.RegularExpressionPlayaround { class MainEntryPoint { static void Main() { Find1(); Console.ReadLine(); } static void Find1() { const string text = @"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."; const string pattern = @"\\bn\\S*ion\\b"; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); WriteMatches(text, matches); } static void Find2() { const string text = @"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."; const string pattern = @"\\bn"; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); WriteMatches(text, matches); } static void WriteMatches(string text, MatchCollection matches) { Console.WriteLine("Original text was: \\n\\n" + text + "\\n"); Console.WriteLine("No. of matches: " + matches.Count); foreach (Match nextMatch in matches) { int index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (index < 5) ? index : 5; int fromEnd = text.Length - index - result.Length; int charsAfter = (fromEnd < 5) ? fromEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; Console.WriteLine("Index: {0}, \\tString: {1}, \\t{2}", index, result, text.Substring(index - charsBefore, charsToDisplay)); } } } }
  • Csharp4 strings and_regular_expressions

    1. 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
    2. 2. Strings and Regular Expressions Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Building strings . Formatting expressions . Using regular expressions
    3. 3. 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
    4. 4. System.String methods
    5. 5. building strings Encoder.cs Encoder2.cs
    6. 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("Hello for all ", 100 ); myBuilder Append(“students.");
    7. 7. 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 );
    8. 8. stringbuilder members cont
    9. 9. Format Strings double d = 16.45; int i = 25; Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i);
    10. 10. Format Strings cont <ul><li>For example, you can include: </li></ul><ul><li>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. </li></ul><ul><li>- 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. </li></ul>
    11. 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. 12. How the string is formatted?
    13. 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. 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. 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. 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. 17. Regular Expressions cont const string pattern = @&quot;bn&quot;; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture );
    18. 18. Regular Expressions patterns
    19. 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. 20. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

    ×