This is the code:
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();
}
The first work in the file that I want to retrieve is Hallo? which is coming right after zethrone1_03510 but between zethrone1_03510 and Hallo? there are two spaces so I’m getting it in the new text file like this Hallo?
And I want it to be Hallo? without the two spaces after zethrone1_03510 that’s one problem.
The second problem is that in the end of the file there is a text -2.8 so I want to retrieve all the text from the first Hallo? including it until the end of the file or until the last -2.8 including it too. Since there are more places after the Hallo? with -2.8
I tried to use LastIndexOf instead of IndexOf but it didn’t work.
And I know there are other ways but I want to do it with my code to fix/repair my code not using another way of codes. Whats wrong here ?
Thanks.
For your first problem you could use the Trim() method to get rid of the space. you could Trim your resulting string like:
(" Hallo").Trim();orvariable.Trim();which results in “Hallo”.LastIndexOf should work with your second problem . You could use int position = f.LastIndexOf (endTag); And after you found the text you should break from the loop so you don’t have an endless loop.
http://msdn.microsoft.com/en-us/library/0w96zd3d.aspx