C# 在c中查找和替换文本#
我需要在程序中添加功能,以便导入的任何文件都能在addTestingPageContentText方法的“”中找到文本,如下所示。每行上的两个值将被添加到datagridview中,datagridview有两列,因此第一列中的第一个文本,第二列中的第二个文本。我怎样才能找到“sometext”C# 在c中查找和替换文本#,c#,regex,import,find,C#,Regex,Import,Find,我需要在程序中添加功能,以便导入的任何文件都能在addTestingPageContentText方法的“”中找到文本,如下所示。每行上的两个值将被添加到datagridview中,datagridview有两列,因此第一列中的第一个文本,第二列中的第二个文本。我怎样才能找到“sometext” 表达式“\”[^”]*\”将查找每个…表达式“\”[^”]*\”将查找每个…既不快速也不高效,但对于那些刚接触正则表达式的人来说更容易理解: while (!endOfFile) { //get
表达式
“\”[^”]*\”
将查找每个…表达式“\”[^”]*\”
将查找每个…既不快速也不高效,但对于那些刚接触正则表达式的人来说更容易理解:
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);
}
}
while(!endOfFile)
{
//获取文件的下一行
字符串行=file.readLine();
编辑://开始时修剪空白
line=line.Trim();
//检查你的字符串
if(line.StartsWith(“addTestingPageContentText”))
{
int start1;
int start2;
//通过找到一个“
对于(start1=0;start1
不过,我还是郑重地建议大家浏览一下互联网上的一些很棒的教程。这是一个非常好的既不快速也不高效,但对于那些不熟悉正则表达式的人来说更容易理解:
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);
}
}
while(!endOfFile)
{
//获取文件的下一行
字符串行=file.readLine();
编辑://开始时修剪空白
line=line.Trim();
//检查你的字符串
if(line.StartsWith(“addTestingPageContentText”))
{
int start1;
int start2;
//通过找到一个“
对于(start1=0;start1
不过,我还是郑重地建议大家浏览一下互联网上的一些很棒的教程。很好注意行首的空格。这将摆脱您的
行。StartsWith(“addTestingPageContentText”)
条件。您还可以用string.IndexOf
替换for
循环的。很好,我假设空格在文章中转换为代码时出错,而不是在数据中。在这种情况下,使用string.Trim()修剪空白将是一个很好的起点。请注意行首的空白。这将摆脱您的行。StartsWith(“addTestingPageContentText”)
条件。您还可以用string.IndexOf
替换for
循环的。很好,我假设空格在文章中转换为代码时出错,而不是在数据中。在这种情况下,使用string.Trim()修剪空白将是一个很好的起点。