一 基于文本的应用
1 控制台应用程序
2 Main()函数的参数-命令行参数
① Main()函数可以带string[]参数;
② Main()函数可以有返回值(int),也可以为void;
二 使用Environment类
CommandLine CommandLineArgs
MachineName OSVersion
UserDomainName UserName
GetEnvironmentVariables
CurrentDirectory SystemDirectory
GEtFolderPath(Environment.SpecialFolder.System)
三 文本处理常用的几个类
1 Console类
Write WriteLine ReadLine
2 String类
3 StringBuilder类
4 System.Text.Encoding类
Default.UTF8 GetEncoding
GetBytes(str) GetString(byte[])
四 正则表达式
1 正则表达式(Regular Expression)
2 用来表示匹配某类文本
如:
[0-9]{2,4}
1+$
3 正则表达式中几个主要要素
3 正则表达式的选项
五 Regex类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace 正则表达式查找电话号码
{
internal class Program
{
static void Main(string[] args)
{
string pattern = @"^[\.a-zA-z]+(?<name>\w+),[a-zA-z]+,[a-zA-z]+,x(?<ext>\d+)$";
string[] sa =
{
"Dr.Dvid Jones,Ophthalmology,x2441",
"Ms.Cindy Harriman,Registry,x6231",
"Mr.Chester Addams,Mortuary,x1667",
"Dr.Hawkeye Pierce,Surgery,x0986",
};
Regex rx = new Regex(pattern);
foreach(string s in sa)
{
Match m = rx.Match(s);
if (m.Success)
Console.Write(m.Result("${ext},${name},%1"));
Console.WriteLine("\t" +
rx.Replace(s, "姓:${name},分机号:${ext}"));
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace 正则表达式常见用法
{
internal class Program
{
static void Main(string[] args)
{
string pattern = "[Bbw]ill";
string s = "My friend Bill will pay the bill";
if (Regex.IsMatch(s, pattern))
Console.WriteLine(s + "与" + pattern + "相匹配");
Regex rx = new Regex(pattern);
MachCollection mc = rx.Match(s);
Console.WriteLine("有{0}次匹配", mc.Count);
foreach(Match mt in mc)
{
Console.WriteLine(mt);
}
Match m = rx.Match(s);
while(m.Success)
{
Console.WriteLine("在位置{0}有匹配'{1}",
m.Index, m.Value);
m = rx.Match(s, m.Index + m.Length);
}
for(m=rx.Match(s);m.Success;m=m.NextMatch())
{
Console.WriteLine("在位置{0}有匹配'{1}",
m.Index, m.Value);
}
}
}
}
播放歌曲显示歌词.rar: https://url09.ctfile.com/f/22158009-755093098-cec3bb?p=5939 (访问密码: 5939)
a-z A-Z ↩︎