查找和替换c#中的文本
本文关键字:文本 替换 查找 | 更新日期: 2023-09-27 18:04:13
我需要在我的程序中添加功能,以便导入的任何文件都可以在addTestingPageContentText方法的"中找到文本,如下所示。然后将每行上的两个值添加到具有2列的datagridview中,因此第一列中的第一个文本然后第二列中的第二个文本。我怎么去找"sometext"呢?
addTestingPageContentText("Sometext", "Sometext");
addTestingPageContentText("Sometext2", "Sometext2");
... continues n number of times.
既不快速也不高效,但对于正则表达式新手来说更容易理解:
while (!endOfFile)
{
//get the next line of the file
string line = file.readLine();
EDIT: //Trim WhiteSpaces at start
line = line.Trim();
//check for your string
if (line.StartsWith("addTestingPageContentText"))
{
int start1;
int start2;
//get the first something by finding a "
for (start1 = 0; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the first something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext1 = line.Substring(start1, start2 - start1);
//get the second something by finding a "
for (start1 = start2 + 2; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the second something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext2 = line.Substring(start1, start2 - start1);
}
}
然而,我强烈建议你去网上看一些很棒的教程。这个很好
表达式"'"[^"]*'""
将找到每个…