正则表达式
在正则表达式中,在使用转义字符“\”时,需要在字符串前加上“@”符号。
string input = "zhangsan@sina.com";
string patten = @"[a-zA-Z]+@[a-zA-Z]+\.com$"; //前需+“@”
Regex r = new Regex(pattern);
Match m = r.match(input);
if(m.success){
}
正则的使用(摘抄的几个比较容易混淆的):
“ab*”:匹配有一个a后面紧跟着0个或若干个b的字符串,如“a”、“ab”、“abb”……
“ab?”:匹配有一个a后面紧跟着0个或1个b的字符串,如“a”、“ab”。
“ab{2}”:匹配有一个a跟着两个b的字符串,即“abb”。
“ab{2,}:匹配有一个a跟着多个b的字符串,即“abb”、“abbb”……。
“a.[0-9]”:匹配有一个a后面跟着一个任意字符和一个数字的字符串。
“^.{3}$”:匹配有任意三个字符的字符串。
“[abcd]”:表示一个字符串中有一个a或b或c或d,相当于“a|b|c|d”或者[a-d]。
如验证URL:
其构造规则为:
[协议]://[www].[域名].[com|net|org]
正则表达式为:
“^http://(www.)(0,1).+\.(com|net|org)$”
输出二维数组好生麻烦!
int[][] Array =new int[][]
{
new int[]{1,3,5,7,9},
newint[]{2,4,6,8,10},
new int[]{1,2}
};
for(int i = Array.GetLowerBound(0); i <= Array.GetUpperBound(0);i++)
{
for(int j = Array[i].GetLowerBound(0); j <= Array[i].GetUpperBound(0); j++)
{
Console.WriteLine(Array[i][j]);
}
}
集合
集合是一组类似的对象。在c#中,任意类型的对象都可以放入一个集合中,并将其视为Object类型。
所在的命名空间:System.Collections
其常用的类或结构如下:
System.Collections中的集合种类
ArrayList
BitArray
Hashtable
Queue
Stack
DictionaryBase
SortedList
可以分为以下3类:
1. 一般集合
一般集合是常见的集合数据结构,包括哈希表、队列、堆栈、字典和列表等。简单说明如下:
列表(ArrayList):一个一维的动态数组,可以装在一组相似的数据元素。
队列(Queue):
堆栈(Stack):
哈希表(Hashtable):集合中的每个元素都是一个<键(key),值(value)>对的列表。
字典(Dictionary):一个<键(key),值(value)>对。
专用集合是具有特定用途的集合,例如StringCollection类,其元素只能为String对象。
3. 位集合
位集合的元素为为标志,每个元素都是一位,而不是一个对象,常用于操作数又包含0和1的集合应用。
主要讲解:
1. ArrayList
常用的形式为:
Public ArrayList();
Public ArrayList(int capacity); // capacity表示含有的元素个数
ArrayListarr1 = new ArrayList();//注:如果不指定,则初始容量为.net的默认值16
ArrayListarr2 = new ArrayList(100);
注:ArrayList中可以存放任何类型的变量,而List类型需要配合模板的使用,并且元素之间必须是同一类型。如:
Debug窗口显示:
ArrayListArrayList1 = newArrayList();
ArrayList1.Add("hello");
ArrayList1.Add(2343);
List<int>List1 = new List<int> () { 15, 45, 487, 789 };
//输出
foreach (objecti in ArrayList1)
{
Console.WriteLine(i);
}
foreach (inti in List1)
{
Console.WriteLine(i);
}
2. Queue类
最主要的方法为入队操作Enqueue和出队操作Dequeue。其他Queue支持的方法,例如遍历、清空等,和ArrayList类似。
常用的形式有:
public Queue();//如果不指定,则初始容量为.net的默认值32
public Queue(int capacity);//初始个数
public Queue(int capacity,float growFactor);//growFator为容量的增长率,默认值为2.0
创建3个对象:
Queue queue1 = new Queue();
Queue queue2 = new Queue(100);
Queue queue3 = new Queue(100, 1.5f);
Queuequeue1=new Queue();
//依次入队:"Hello"、、{1,2,3}
queue1.Enqueue("Hello");
queue1.Enqueue(1);
queue1.Enqueue(newint[]{1,2,3});
//通过出队操作,输出队列中的所有元素,这个顺序,与入队的顺序一致
objectoutItem=new object();
while(queue1.Count>0)
{
outItem=queue1.Dequeue();
Console.WriteLine("{0}",outItem);
}
注:Queue也是可以赋上任何类型的值。
3. Stack类
常用的形式
:
public Stack();//默认为10
public Stack(int capacity);
如:
Stack Stack1 = new Stack();
Stack Stack2 = new Stack(100);
常用操作:
Stack1.push();
Stack1.pop();
4. Hashtable类和 SortList类