首页 新闻 搜索 专区 学院

真心求助,字符串解析问题!!

0
悬赏园豆:15 [已解决问题] 解决于 2012-07-05 11:42

--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx
Content-Disposition: form-data; name="expressDealId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

111209511524_1
--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx
Content-Disposition: form-data; name="expressCompanyId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

wu023shangcheng
--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx--
例如上述字符串,怎样能获取到
name="expressDealId" 111209511524_1
name="expressCompanyId" wu023shangcheng

lingxiayidu的主页 lingxiayidu | 初学一级 | 园豆:31
提问于:2012-04-24 14:59
< >
分享
最佳答案
0
目测,应该可以用这个代码
View Code
public void test()
        {
            string s = "";
            int start = 0;
            List<data> list = new List<data>();
            do
            {
                int index = s.IndexOf("name=\"", start);                
                start = index + 6;
                if (start <= 6 || start > s.Length) break;
                int end = s.IndexOf("\"", start);
                if (end < 0) break;
                data item = new data();
                item.Name = s.Substring(start, end - start);
                start = end;
                index = s.IndexOf("\r\n\r\n", start);
                start = index + 4;
                end = s.IndexOf("\r\n", start);
                if (start <= 4 || end < 0) break;
                item.Value = s.Substring(start, end - start);
                list.Add(item);
            } while (start > 6);
            
        }

public class data
    {
        public string Name { set; get; }
        public string Value { set; get; }
    }
收获园豆:15
sinhbv | 老鸟四级 |园豆:2579 | 2012-04-24 15:47
其他回答(3)
0

没看明白啥意思?

雨之秋水 | 园豆:645 (小虾三级) | 2012-04-24 15:02

就是说怎么样解析,可以得到这个结果name="expressDealId" 111209511524_1
name="expressCompanyId" wu023shangcheng 

支持(0) 反对(0) lingxiayidu | 园豆:31 (初学一级) | 2012-04-24 15:21
0

先用换行符把字符串分解成字符串数组,FOR循环,然后如果有name关键字就截取name的值(最好是正则表达式),然后I+3(或者判断Content-Transfer-Encoding关键字的下一个循环)取值

硕硕 | 园豆:212 (菜鸟二级) | 2012-04-24 17:33
0

用正则表达式实现的C#代码如下:

[Test]
public void RegexTest()
{
    var text = System.IO.File.ReadAllText("f:\\temp\\text.txt");
    var matches = Regex.Matches(text, 
        @"(name=""[^""]+"")(?:\r\n[^\r\n]+){2}\r\n([^\r\n]+)");
    foreach (Match match in matches)
    {
        Console.WriteLine(match.Groups[1] + " " + match.Groups[2]);
    }
}
dudu | 园豆:38992 (高人七级) | 2012-04-24 20:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册