zoukankan      html  css  js  c++  java
  • C#博客第六周

    Regular expressions

    一.关键概念

    1.The result of applying a regular expression to a string is one of the following: To find out whether the string matches the regular expression. To return a substring. To return a new string representing a modification of some part of the original string.

    2.System.Text.RegularExpressions namespace is the home of objects associated with regular expressions.

    二.运行实例

    1.代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Text.RegularExpressions;
    namespace study5
    {
    class Program
    {
    static void Main(string[] args)
    {
    string string1 = "This is a test string"; // find any nonwhitespace followed by whitespa Regex theReg = new Regex(@"(S+)s"); // get the collection of matches MatchCollection theMatches = theReg.Matches(string1); // iterate through the collection foreach (Match theMatch in theMatches) { Console.WriteLine("theMatch.Length: {0}", theMatch.Length); if (theMatch.Length != 0) { Console.WriteLine("theMatch: {0}", theMatch.ToString( )); } }
    }
    }
    }

    2.输出结果

    theMatch.Length: 5
    theMatch: This
    theMatch.Length: 3
    theMatch: is
    theMatch.Length: 2
    theMatch: a
    theMatch.Length: 5
    theMatch: test
    请按任意键继续. . .

    Exceptions

    1.关键字 try catch finally throw

    2.示例代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Text.RegularExpressions;

    namespace study5
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {
    Console.WriteLine("Entering try block...");
    throw new System.ApplicationException();
    Console.WriteLine("Exiting try block...");
    }
    catch
    {
    // Note: simplified example, it is not really solve the exception.
    Console.WriteLine("Exception caught and handled.");
    }
    }

    }

    }

    3.输出结果

    System.DivideByZeroException: Msg: Divided 0
    在 study5.Program.DoDivide(Double a, Double b) 位置 c:Users史航宇Documents
    Visual Studio 2013Projectsstudy5study5Program.cs:行号 32
    在 study5.Program.Main(String[] args) 位置 c:Users史航宇DocumentsVisual S
    tudio 2013Projectsstudy5study5Program.cs:行号 14
    Close file here.
    请按任意键继续. . .

    Delegates and Events

    1.示例代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    namespace ConsoleApplication5
    {
    class Program
    {
    static int counter = 0;
    static string displayString = "World,I love you! 世界,我喜欢你! ";
    static void Main(string[] args)
    {
    Timer myTimer = new Timer(100);
    myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
    myTimer.Start();
    Console.ReadKey();

    }
    static void WriteChar(object source, ElapsedEventArgs e)
    {
    Console.Write(displayString[counter++ % displayString.Length]);

    }
    }
    }

    2.输出结果

    World,I love you!
    世界,我喜欢你!
    (一个字一个字的出现)

  • 相关阅读:
    Web前端开发工程师编程能力飞升之路
    commonJSAMDCMD
    js正则替换十六进制
    JS获取粘贴内容
    ajax跨域访问总结
    需要坚持,能写出自己满意的内容
    【新年献词】我们是南方周末,我们三十而立
    iOS: 动态更改 backBarButtonItem 的 title
    iOS: 计算 UIWebView 的内容高度
    iOS: 在键盘之上显示一个 View
  • 原文地址:https://www.cnblogs.com/Theshy/p/4442287.html
Copyright © 2011-2022 走看看