C# 如何使用IndexOf和SubString从文件中检索文本?它是';行不通
代码如下:C# 如何使用IndexOf和SubString从文件中检索文本?它是';行不通,c#,C#,代码如下: private void retrivingText1() { string startTag = "zethrone1_03510";//"<Translation>"; string endTag = "-2.8";//"</Translation>"; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; index = 0; w
private void retrivingText1()
{
string startTag = "zethrone1_03510";//"<Translation>";
string endTag = "-2.8";//"</Translation>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
index = 0;
w = new StreamWriter(@"d:\retrivedText1.txt");
while (true)
{
index = f.IndexOf(startTag, index);
if (index == -1)
{
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
index = f.IndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start);
w.WriteLine(g);
}
w.Close();
}
private void retrievingText1()
{
字符串startTag=“zethrone1_03510”/”;
字符串endTag=“-2.8”/”;
int startTagWidth=startTag.Length;
int endTagWidth=endTag.Length;
指数=0;
w=新的StreamWriter(@“d:\RetrievedText1.txt”);
while(true)
{
索引=f.IndexOf(startTag,index);
如果(索引==-1)
{
打破
}
//还有更多的事情要做-索引现在定位在startTag的第一个字符处
int start=index+startTagWidth;
索引=f.IndexOf(endTag,start+1);
如果(索引==-1)
{
打破
}
//找到了endTag
字符串g=f.Substring(start,index-start);
w、 书写线(g);
}
w、 Close();
}
我要检索的文件中的第一个工作是Hallo?
,它紧跟在zethrone1_03510
之后,但是在zethrone1_03510
和Hallo?
之间有两个空格,所以我在新的文本文件中得到它,就像这样Hallo?
我希望它是Hallo?
没有zethrone1\u 03510
后面的两个空格,这是一个问题
第二个问题是,在文件的末尾有一个文本-2.8
,因此我想从第一个Hallo?
中检索所有文本,包括它,直到文件的结尾,或者直到最后一个-2.8
也包括它。因为有了-2.8
我尝试使用LastIndexOf
而不是IndexOf
,但没有成功
我知道还有其他方法,但我想用我的代码来修复/修复我的代码,而不是使用其他代码方法。这里怎么了
谢谢。对于您的第一个问题,您可以使用Trim()方法去除空间。您可以修剪生成的字符串,如:
(“Hallo”).Trim()代码>或变量.Trim()代码>这会导致“你好”
LastIndexOf应该可以解决第二个问题。可以使用int position=f.LastIndexOf(endTag);找到文本后,你应该从循环中中断,这样你就不会有一个无休止的循环
private void retrievingText1()
{
字符串startTag=“zethrone1_03510”/”;
字符串endTag=“-2.8”/”;
int startTagWidth=startTag.Length;
int endTagWidth=endTag.Length;
指数=0;
w=新的StreamWriter(@“d:\RetrievedText1.txt”);
while(true)
{
索引=f.IndexOf(startTag,index);
如果(索引==-1)
{
打破
}
//还有更多的事情要做-索引现在定位在startTag的第一个字符处
int start=index+startTagWidth;
index=f.LastIndexOf(endTag,start+1);
如果(索引==-1)
{
打破
}
//找到了endTag
string g=f.Substring(start,index-start+endTagWidth).Trim();//修剪已建立的文本,以便删除开头和结尾空格。
w、 书写线(g);
//打破这样你就不会有一个无休止的循环
打破
}
w、 Close();
}
您是否因为编写标记而使用XML文件?变量f是什么?您使用f.IndexOf(startTag,index)代码>但我从未看到f声明。SynerCode nope startTag和endTag是,因为我以前在另一个文件中使用过它。但是我现在读取的文件i.BIN和变量f是在顶级表单中声明的,我在构造函数中使用它来读取.BIN文件,直到最后。f是一个字符串变量。第二个问题是,在“Hallo?”之后有很多文本,文本结尾我指的是文件,文本结尾是“-2.8”,而-2.8是文本的一部分。我想检索从“Hallo?”到“-2.8”的所有文本,包括-2.8。问题是,在“Hallo?”和文件结尾之间,有更多的地方是“-2.8”这个词,所以它是从“Hallo?”检索文本,直到他找到第一个“-2.8”,而不是直到文本/文件结尾的最后一个“-2.8”。因此,他只重读了课文的一部分,直到最后才重读。我将其更改为string startTag=“喂?”.Trim();它根本就没有表现出“哈罗?”的意思,威奇是不好的。在此之前,我做了字符串startTag=“zethrone1_03510”.Trim();但是它没有解决它。SynerCode这行索引=f.LastIndexOf(endTag,start+1);在编辑的示例中,使其进入IF并执行中断;因此,创建的文件是空的。
private void retrivingText1()
{
string startTag = "zethrone1_03510";//"<Translation>";
string endTag = "-2.8";//"</Translation>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
index = 0;
w = new StreamWriter(@"d:\retrivedText1.txt");
while (true)
{
index = f.IndexOf(startTag, index);
if (index == -1)
{
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
index = f.LastIndexOf (endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start + endTagWidth).Trim(); //Trim the founded text so the start and ending spaces are removed.
w.WriteLine(g);
//break so you dont have an endless loop
break;
}
w.Close();
}