Jump to content
  • Advertisement
Sign in to follow this  
nb2

Problem with String Comparison c++ .NET

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am constructing a program to read text from a webpage, filter out the html tags and common words ("the", "as", "and", etc.) and output the filtered text to a text file. I'm having trouble filtering out the common words, however. I think it's just the basic string comparison. Here is the function:
String* filter_cw(String* text_string)
{
StreamReader* infile = new StreamReader("common.txt");
ArrayList* cw_list = new ArrayList();
String* word = "";

//Store all common words into array cw_list
word = infile->ReadLine();
while(word!=0)
{
cw_list->Add(word);
word =  infile->ReadLine();
}
infile->Close();

Char chars[] = {' ', ',', '.', ':', '-', '\n'};
String* word_array[] = text_string->Split(chars);
IEnumerator* listEnum = word_array->GetEnumerator();
//StringBuilder filtered_text;  not in use yet
bool isCommon;

while(listEnum->MoveNext())
{
isCommon = false;
String* s = listEnum->current->ToString();
for(int i=0;i<cw_list->Count;i++)
{
String* cw = cw_list->Item->ToString();
if(String::Compare(s,cw,true)==0) //DOES NOT WORK
{
Console::WriteLine(s);
break;
}
}
}
return "random string";  
}
There are no compiler errors, everything seems to work except the comparison. So, this function takes in a string of text - it reads the list of common words from a file and stores them in an array. Then it splits the string that was passed to it into an array of strings divided at punctuation marks and whitespaces. It then should compare each word from the string to each common word, however, no string comparison i use works, i've tried String::Compare, String::Equals, ==, and at one point made my own string comparison function that compared character by character, nothing worked. It just says that none of the words match. I'm very new to .NET, been working on this for a while and can't figure it out, any help would be very much appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
readline returns a String^ not a String* the difference is String^ is allocated in memory controlled by the garbage collector and String* isn't it may be getting collected before you get to compare it. Either way they are not compatable types.

Share this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
readline returns a String^ not a String* the difference is String^ is allocated in memory controlled by the garbage collector and String* isn't it may be getting collected before you get to compare it. Either way they are not compatable types.


Thank you for answering - a few questions though.
1) If they were not compatible types, wouldn't that have caused an error?
2) From all the resources I have seen, it has seemed like String* in 2003 is the same as String^ in 2005. Using the String* in all of the StreamReader objects I used seemed to work fine.
3) If what you said is the case, and they aren't compatible or if the strings are being collected before the comparison, what would be the code to compare?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!