Jump to content

file searching and indexing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
aditya

aditya

    Newbie

  • Members
  • Pip
  • 1 posts
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?

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Use the System.IO namespace to communicate with the file system.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
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
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
I didn't know C# was capable of that; quite an interesting feature.