c# Large amount of text to divide into blocks
c# I have a large block of text. this text is divided into units of comments the text looks like this (In order for me not to waste time explaining something you can see)
MikeSmith randdesign chapter 1 paragraph 1 book randdesign a:) comments on the book are futile
Johnblack randdesign chapter 1 paragraph 1 book randdesign a:) comments on the book are futile if the book is left open and sometimes these lines can be multiple and long comments
willsmith randdesign chapter 1 paragraph 1 book randdesign a:) no comments on the book are futile
As you can see this block of text is interesting as I am a beginer I have very little experiance in coding, I have c# 3.0 cookbook and I am trying to apply the Finding the location of all occurences of a string within another string.
there are a few constants I can use
1 the user names will always be the same. (willsmith)and I have 20 users.
2 the structure will always begin with the username
3 the actual content that I wish to import will always start with a:) b:) c:)
My question(s) are the following
a:) is Finding the location of all occurences of a string within another string, the best method of finding the begining and the en开发者_如何学运维d of my data?
b:) is there a quicker way I can rip the data from the text?
c:) sometimes a user will quote another user, if I look for names then I could get false positives. how do i deal with that?
Thanks for the help
a: Yes, this is very easily done. Assuming your block of text is one huge string, and that string is called 's', then you could split the text by the new line character, loop through each line, and grab the comments, as follows:
string s = @"
MikeSmith randdesign chapter 1 paragraph 1 book randdesign a:) comments on the book are futile
Johnblack randdesign chapter 1 paragraph 1 book randdesign a:) comments on the book are futile if the book is left open and sometimes these lines can be multiple and long comments
willsmith randdesign chapter 1 paragraph 1 book randdesign a:) no comments on the book are futile
";
foreach (string line in s.Split('\n'))
{
if (line.Trim() != "")
{
string comments = line.Substring(line.IndexOf("a:) ") + 4);
}
}
b: Perhaps, but this will be pretty speedy for you.
c: Try altering your IndexOf() method if you need to. You can tailor it to look for anything, such as 'b:)', and 'c:)', as you mentioned above.
精彩评论