我有一个文本文件,读取:
INSERT INTO `shops` VALUES ('','3','1000000','0'); INSERT INTO `shops` VALUES ('','1000010','1000020','1000030','1001000','0');
请注意每行第一个键是”.对于每一行,我想找到”,并用数字(从1开始)替换它,然后在它进入下一行时加1,如下所示:
INSERT INTO `shops` VALUES ('1','0'); INSERT INTO `shops` VALUES ('2','0'); INSERT INTO `shops` VALUES ('3','0'); INSERT INTO `shops` VALUES ('4','0'); INSERT INTO `shops` VALUES ('5','0');
我一直试图这样做几个小时,但我失败了.
这就是我一直在想的(我知道这远非正确,但我在c#中并不精通,所以也许你们中的一个可以帮我提出正确的代码):
string text = File.ReadAllText("C:\\Users\\Donavon\\Desktop\\old.sql"); int i = 0; text = text.Replace("('',","('" + i + "',"); i++; File.WriteAllText("C:\\Users\\Donavon\\Desktop\\new.sql",text);
感谢您的帮助,非常感谢
解决方法
您可以单独阅读这些行:
string text = ""; using (StreamReader sr = new StreamReader("C:\\Users\\Donavon\\Desktop\\old.sql")) { int i = 0; do { i++; string line = sr.ReadLine(); if (line != "") { line = line.Replace("('',"); text = text + line + Environment.NewLine; } } while (sr.EndOfStream == false); } File.WriteAllText("C:\\Users\\Donavon\\Desktop\\new.sql",text);