Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with String Comparison c++ .NET

Started by nb2 Nov 26, 2008 at 4:34 PM 1 replies 800+ views
Original Post
nb2
nb2
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.
stonemetal
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.
nb2
nb2
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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.