hi i am new to c#
i have gone through the varous features of c#
i now want to make a text file searcher which should also index the words in a separate file.
how do i go about it?
file searching and indexing
Started by aditya, May 18 2008 01:11 AM
3 replies to this topic
#1
Posted 18 May 2008 - 01:11 AM
|
|
|
#3
Posted 18 May 2008 - 08:41 AM
Do you want it to count the number of times a word occurs and index that in a separate file? If thats the case it shouldn't be to difficult. You need to use a stream reader to get the text from the file and then this code should be close to what u want except you need to rewrite the returns into another text file using a stream writer.
public static int CountWords(string strText, bool stripTags)
{
// Declare and initialize the variable holding the number of counted words
int countedWords = 0;
// If the stripTags argument was passed as false
if (stripTags == false)
{
// Simply count the words in the string by splitting them wherever a space is found
countedWords = strText.Split(' ').Length;
}
else
{
// If the user wants to strip tags, first define the tag form
Regex tagMatch = new Regex("<[^>]+>");
// Replace the tags with an empty string so they are not considered in count
strText = tagMatch.Replace(strText, "");
// Count the words in the string by splitting them wherever a space is found
countedWords = strText.Split(' ').Length;
}
// Return the number of words that were counted
return countedWords;
}
This method takes an overload of bool type indicating weather or not you wish to strip the tags on words and include these matches as well. Also when you use the text reader you need to pass it to a string called strText. Hopes this helps...If its not what your looking for srry.
Edited by gaylo565, 18 May 2008 - 09:10 AM.
#4
Posted 18 May 2008 - 03:17 PM
I didn't know C# was capable of that; quite an interesting feature.


Sign In
Create Account

Back to top









