码迷,mamicode.com
首页 > Windows程序 > 详细

C#博客第六周

时间:2015-04-20 20:35:43      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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 2013\Projects\study5\study5\Program.cs:行号 32
在 study5.Program.Main(String[] args) 位置 c:\Users\史航宇\Documents\Visual S
tudio 2013\Projects\study5\study5\Program.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!\n世界,我喜欢你!\n";
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!
世界,我喜欢你!
(一个字一个字的出现)

C#博客第六周

标签:

原文地址:http://www.cnblogs.com/Theshy/p/4442287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!