Now after I solved by help here getting the text from between two tags in a small text file that I created for testing with only 4 lines. Now I want to create a new text file that will contain the content of the original file but in every place where I found the text between the tags I want to see spaces empty string. So if the original text file is looks like this now:
daniel<Text>THISISOUTisthere</Text>
<Text>hellobye</Text>
<Text>danielTHISishereandnotthere</Text>
danie <Text> is T开发者_开发技巧HIS here and not THERE</Text>
So the new file should looks like:
daniel<Text> </Text>
<Text> </Text>
<Text> </Text>
danie <Text> </Text>
Here is the code that doesn't work now. I used some help variables but I'm getting an error when running this code on the line:
string hh = f.Substring(lastIndex, currentIndex);
The error say: Index and length must refer to a location within the string. Parameter name: length
This is the complete code now which doesn't work:
private void test()
{
w = new StreamWriter(@"d:\testFile.txt");
int currentLength;
int currentIndex;
int lastIndex = 0;
string startTag = "<Text>";
string endTag = "</Text>";
int startTagWidth = startTag.Length;
//int endTagWidth = endTag.Length;
index = 0;
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;
currentIndex = start;
index = f.IndexOf(endTag, start+1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start - 1);
currentLength = index - start - 1;
string hh = f.Substring(lastIndex, currentIndex);
w.WriteLine(hh);
lastIndex = currentIndex + currentLength;
listBox1.Items.Add(g);
}
}
Please help me with this code.
substring takes a length as its second parameter not a index position so it should be
string hh = f.Substring(lastIndex, currentIndex-lastIndex);
additionally you are chopping too many characters off, you want to change currentLength = index - start -1
to be currentLength = index - start
And finally using Writeline
will be puttin addtional line feeds in, use Write
instead.
For a fun alternative, you could use a regular expression to do the replacement for you:
string input = "daniel<Text>THISISOUTisthere</Text>\n<Text>hellobye</Text>\n<Text>danielTHISishereandnotthere</Text>\ndanie <Text> is THIS here and not THERE</Text>";
Regex re = new Regex("(?<=<Text>).*?(?=</Text>)");
string output = re.Replace(input, m => new string(' ', m.Length));
Console.WriteLine(input);
Console.WriteLine();
Console.WriteLine(output);
Program output:
daniel<Text>THISISOUTisthere</Text>
<Text>hellobye</Text>
<Text>danielTHISishereandnotthere</Text>
danie <Text> is THIS here and not THERE</Text>
daniel<Text> </Text>
<Text> </Text>
<Text> </Text>
danie <Text> </Text>
Another fun alternative, as I felt like I wanted to try to get it to work: A small do-it-yourself parser.
Note: This is by far not a real HTML or XML parser! It only involes one single tag (e.g. <Text>
) and only without any attributes...
So, what do you need for a parser? Right, a tokenizer. Here you go:
static IEnumerable<Token> Tokenize(string input, string tag)
{
int index = 0;
int lastIndex = 0;
// Define the start and end tag and their common first character
char tagChar = '<';
string startTag = tag + '>';
string endTag = '/' + tag + '>';
while (true)
{
Token token = null;
// Search for any new tag token
index = input.IndexOf(tagChar, index) + 1;
if (index <= 0)
break;
// Starttag or endtag token found
if (input.Substring(index, startTag.Length) == startTag)
token = new Token { Start = index - 1, Length = startTag.Length + 1, TypeOfToken = Token.TokenType.StartTag };
else if (input.Substring(index, endTag.Length) == endTag)
token = new Token { Start = index - 1, Length = endTag.Length + 1, TypeOfToken = Token.TokenType.EndTag };
// Yield the text right before the tag and the tag itself
if (token != null)
{
yield return new Token { Start = lastIndex, Length = index - lastIndex - 1, TypeOfToken = Token.TokenType.Text };
yield return token;
lastIndex = index + token.Length - 1;
}
}
// Yield last text token
yield return new Token { Start = lastIndex, Length = input.Length - lastIndex, TypeOfToken = Token.TokenType.Text };
}
class Token
{
public int Start { get; set; }
public int Length { get; set; }
public TokenType TypeOfToken { get; set; }
public enum TokenType
{
Text,
StartTag,
EndTag
}
}
It is even somewhat optimized, as it only searches for <
and checks if it is a start or end tag afterwards.
Having the string tokenized, the rest of the processing is quite simple:
static string ProcessString(string input, string tag)
{
var sb = new StringBuilder();
int depth = 0;
foreach (var token in Tokenize(input, tag))
{
// Append all tags, but only text tokens with depth level 0
if (token.TypeOfToken != Token.TokenType.Text ||
(token.TypeOfToken == Token.TokenType.Text && depth == 0))
sb.Append(input.Substring(token.Start, token.Length));
else
sb.Append(new string(' ', token.Length));
// Increment for each starttag, decrement for each endtag, never smaller than 0
depth = Math.Max(0, depth + (token.TypeOfToken == Token.TokenType.StartTag ? 1 :
(token.TypeOfToken == Token.TokenType.EndTag ? -1 : 0)));
}
return sb.ToString();
}
This is quite a bit more flexible than the regex solution, because you can give it more semantic meaning, like the depth. For example calling this:
ProcessString("level0<Tag>level1<Tag>level2</Tag>level1again</Tag>level0again", "Tag");
will be processed to this:
"level0<Tag> <Tag> </Tag> </Tag>level0again"
精彩评论