点点滴滴访问量:
///普通格式命令的解析
            //安空格解析字符串,并单独显示子串出来
            Console.WriteLine("请输入以空格分开的三个字符串:");
            
string allStr = Console.ReadLine();//"I am zsp.";
            char[] c = new char[] ' ','a' };
            
string[] str = allStr.Split(c);
            
string str1 = str[0];
            
string str2 = str[1];
            
string str3 = str[2];
            Console.WriteLine(str1);
            Console.WriteLine(str2);
            Console.WriteLine(str3);
           
            
///特殊格式命令解析方法一:
            
///方法一说明:
            
///首先在字符串中寻找“EOF”
            
///以确定总的有效字符串长度
            
///然后分别查找“\r\n”的位置,最后分段将数据赋值给字符串对像

            string readMessage = "user" + "\r\n" + "AAA" + "\r\n" + "PASS" + "\r\n" + "BBB" + "EOF";
            
int x = readMessage.IndexOf("EOF");
            
string ss = readMessage.Substring(0, x);
            
int y = ss.IndexOf("\r\n");
            Console.WriteLine(y.ToString());

            
///取得第一个子串:user
            string command = ss.Substring(0, y);//取得第一个子串,这里指得是“user”
            Console.WriteLine("取得第一个子串:" + command);

            
string aaa = ss.Substring(y + 2, x - y - 2);//剩下的是一个子串
            
//Console.WriteLine(aaa + "***" + "");

            
///取得第二个子串:AAA
            y = aaa.IndexOf("\r\n");
            Console.WriteLine(y.ToString());
            
string command1 = aaa.Substring(0, y);//把剩下的子串做为一个字符串,并取得第一个子串,这里指得是“AAA”
            Console.WriteLine("取得第二个子串:" + command1);

            aaa 
= aaa.Substring(y + 2, aaa.Length - y - 2);

            
///取得第三个子串:PASS
            y = aaa.IndexOf("\r\n");
            Console.WriteLine(y.ToString());
            
string command2 = aaa.Substring(0, y);
            Console.WriteLine(
"取得第三个子串:" + command2);
            
//Console.WriteLine(aaa);

            
///取得第四个子串:BBB
            y = aaa.IndexOf("\r\n");
            Console.WriteLine(y.ToString());
            aaa 
= aaa.Substring(y + 2, aaa.Length - y - 2);
            Console.WriteLine(
"取得第四个子串:" + aaa);


            
///特殊格式命令解析方法二:
            
///方法二说明:
            
///首先构造一个RichTextBox控件对像
            
///将有效字符串的值赋给该控件
            
///然后再在该控件中按行读取数据

            //string readMessage = "user" + "\r\n" + "AAA" + "\r\n" + "PASS" + "\r\n" + "BBB" + "EOF";
            
//RichTextBox rich = new RichTextBox();
            
//rich.Text = readMessage;
            
//int i = rich.Lines.Length;
            
//string comm = rich.Lines[0];
            
//string comm1 = rich.Lines[1];
            
//string comm2 = rich.Lines[2];
            
//string comm3 = rich.Lines[3].Substring(0, rich.Lines[3].Length - 3 );
            
//Console.WriteLine("{0}  {1}  {2}  {3}",comm,comm1,comm2,comm3);
posted on 2007-03-07 18:40  sopper  阅读(691)  评论(0编辑  收藏  举报