提取文本文件内容
如何提取“=”后面的内容&L
WK_PRDNO=5073800123
WK_NAME=张三
正常= 0.00
逾期= 12.21
正常利息= 0.00
罚息= 0.04
复利= 0.11
&L
WK_PRDNO=5073800123
WK_NAME=李四
正常= 0.00
逾期= 12.21
正常利息= 0.00
罚息= 0.04
复利= 0.11
&L
很急呀,请大虾们帮帮忙!!! 谢谢了!!!
很急呀,请大虾们帮帮忙!!! 谢谢了!!! 逐行读取数据,然后用split('=')分开能给出具体代码吗?
要求把每行具有相同属性的内容存到一个文件里,如每行WK_NAME的内容张三
李四 可以将读取到的内容用split()处理后添加到NameValueCollection中。
例:
NameValueCollection myCol = new NameValueCollection();
myCol.Add("WK_NAME", "张三");
myCol.Add("WK_NAME", "李四");
Console.WriteLine(myCol["WK_NAME"]);
运行结果为:张三,李四 public static void Main()
{
FileStream fs = null;
NameValueCollection dataCol = new NameValueCollection();
string sourceFile = @"C:\data.txt";
if (!File.Exists(sourceFile))
{
Console.WriteLine("Source File Not Found!");
Console.ReadLine();
return;
}
//Opens the source file.
fs = new FileStream(sourceFile, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string line;
//A value is read from the file.
while (sr.Peek() > -1)
{
line = sr.ReadLine();
if (line.Contains('='))
{
string[] data = line.Split('=');
dataCol.Add(data[0].Trim(), data[1].Trim());
}
}
sr.Close();
fs.Close();
//create a new txt file for each property
foreach (string key in dataCol.AllKeys)
{
//check directory
if (!Directory.Exists(@"C:\test"))
Directory.CreateDirectory(@"C:\test");
//open the new file, if not exists, create it
StreamWriter newFile = File.AppendText(string.Format(@"C:\test\{0}.txt", key));
//get all value with the same property
string[] keyValue = dataCol[key].Split(',');
//write values to the file
foreach (string value in keyValue)
newFile.WriteLine(value);
newFile.Close();
}
}
[[it] 本帖最后由 slokra 于 2008-6-25 09:59 编辑 [/it]] [em10] 可以转换成中文注释吗
编译报错
if (line.Contains('=')){
string[] data = line.Split('=');
报错信息
error CS0117: “string”并不包含对“Contains”的定义 你的.net是哪个版本的?
string.Contains()是2.0新增的 string.Split('=')
这个可以噻 split的方法可以是可以,但是局限性很大,最好的方法是用正则表达式来取。 啊哦 用于检测=的正则表达式我知道不是很复杂 呵呵
不过 嘿嘿
我写不来正则表达式
呵呵 不好意思
给你解决不了
用最新的编译器还是不行???
适用于 Microsoft(R) .NET Framework 3.5 版的 Microsoft(R) Visual C# 2008 编译器 3.5.21022.8 版版权所有 (C) Microsoft Corporation。保留所有权利。
1.cs(30,21): error CS1502:
与“string.Contains(string)”最匹配的重载方法具有一些无效参数
1.cs(30,35): error CS1503: 参数“1”: 无法从“char”转换为“string” 晕晕 将那个char值ToString()一下嘛
页:
[1]
