New to Linux, std::string problem

Hi all this is my first topic and post and only my second day using Linux, so here goes.

I recently installed Ubuntu 10.10 and Im in the process of migrating a C++ console simulation to Ubuntu from Windows.

I have a class with an std::string member variable which is returned by a getStringMember() method. Now I need to compare this returned value for equality with the string "Buy".

the code simply looks like this:

std::string returnedString = object.getStringMember();
std::string comparisonString = "Buy";

When returnedString is "Buy" the two strings should be equal, which is the case in my Windows version. However in Ubuntu and using Code::Blocks with gcc the returned string has always a size that is one larger than it ought to. Ive checked that there is no trailing spaces attached.

Does Linux encode std::strings differently and if so what do i need to do for the code to perform as required?

Thanks very much for your time.
There may be some non printable chars in there.
Character encoding shouldn't matter
There is a couple of things you can try to determine the problem.

1/ run size on both strings
std::cout << "Returned string size is " << returnedString.size() << std::endl;
std::cout << "ComparisonString size is " << comparisontring.size() << std::endl;

If the sizes are different the try removing whitespace chars from each string

2/ First with returnedString
std::string::size_type index;

index = returnedString.find_last_not_of(" \t\n");
returnedString.erase(index+1); // trim trailing whitespace

index = returnedString.find_first_not_of(" \t\n");
returnedString.erase(0,index); // trim leading whitespace

Try your program now.....

2/ Next with comparisonstring

index = comparisonstring.find_last_not_of(" \t\n");
comparisonstring.erase(index+1); // trim trailing whitespace

index = comparisonstring.find_first_not_of(" \t\n");
comparisonstring.erase(0,index); // trim leading whitespace

Try your progam again. Both strings should be same size and should work.

~
Start by posting the comparing code...
Topic archived. No new replies allowed.