309019,fhn4guxnj3baupp401o5awfg,/search/?keyword=名品打折网,,2012-07-02 10:16:38.580,2012/7/2,GET,0,61.49.0.229,Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA; SE 2.X MetaSr 1.0)求教高师指点,怎么样把名品打折网取出来 放在一个文件中,就是读一个多行类似的代码,然后把=和,之间的东西取出来放到一个文档中!

解决方案 »

  1.   

    string str = "/?keyword=名品打折网,,2012-07-02 10:16:38"
    如果只有一个=号或者这个=号一定是第一个出现的=号的话
     先得到“=”的位置
    int index= str.indexof('=') 
    前面可能有=号的话 看能不能用  indexof("keyword=")   
    将=号前面的字符去掉 str=str.SubString(index+1)
    再得到第一个“,”的位置  消掉该位置以后的字符 
    str=str.Remove(str.IndexOf(',')) 即可得到 “名品打折网”
      

  2.   


                string source =
                    "309019,fhn4guxnj3baupp401o5awfg,/search/?keyword=名品打折网,,2012-07-02 10:16:38.580,2012/7/2,GET,0,61.49.0.229,Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA; SE 2.X MetaSr 1.0)";
                string Pattern = "keyword=([^,]+)";
                var result = Regex.Match(source, Pattern).ToString().Replace("keyword=","");
      

  3.   


            /// <summary>
            /// 读取文本文件内容
            /// </summary>
            /// <param name="filePath">文件路径绝对路径,如 C:\1.txt</param>
            /// <returns></returns>
            public static string ReadTextFile(string filePath)
            {
                string strFileContent = "";
                if (File.Exists(filePath))
                {
                    strFileContent = File.ReadAllText(filePath);
                }
                return strFileContent;
            }        /// <summary>
            /// 将文件内容存入filePath指定文件,替换原有内容,若文件不存在,则新建文件
            /// </summary>
            /// <param name="filePath">文件绝对路径</param>
            /// <param name="content">文件内容</param>
            /// <returns></returns>
            public static bool ModifyFile(string filePath, string content)
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                using (FileStream fs = File.Create(filePath))
                {
                    Byte[] file = new UTF8Encoding(true).GetBytes(content);
                    fs.Write(file, 0, file.Length);
                }
                return File.Exists(filePath);
            }