-
trimming strings...
hi ppl...sorry for asking this again but im having a horrible time trying to prune this string...the string in 'word' could be anything like below: (ive put quotes so you know that there are spaces...otherwise there are no quotes in the string 'word'
Code:
'*CLASS* alt yes no '
'*CLASS* alt hello right'
i use getline to read in a line into word...now i need to extract only the words from the string excluding all spaces or newlines etc...
someone told me about using find() and then substr() but i don't know the size of the words...can be 3 letters or 2 or anything...
could someone show me how to use it...this is the code i have so far:
Code:
ifstream in(filename);
in >> word; //just to pick up the word *CLASS*
//then i check if the word is *CLASS*
//if it is then:
in.getline(word); //this gets the whole line after *CLASS*
//now i want to extract one word at a time and then do some operation on it
//each word i extract should not have any spaces in it...
while //what should be the condition in while?
{
word.substr(1,word.find(' '))
//then use the word
}
Regards,
Farooq
-
Use of a stringstream object would seem easiest:
Code:
#include <sstream>
#include <string>
...
std::string word = "*CLASS* alt hello right";
std::string temp;
std::istringstream parser(word);
while( parser >> temp )
std::cout << temp << std::endl;
Should output something like:
Code:
*CLASS*
alt
hello
right
All neatly seperated out according to the spaces in the original string.
-
thanks for the reply...
what if your string was like this:
Code:
std::string word = "*CLASS* alt hello right ";
i.e. not ending with a newline but with a space and then newline...
or maybe with more than one space between words?
i would like to use your methods because it seems a lot more simpler than playing around with substrings...
Regards,
Farooq
-
Got your PM...
I had a test program set up to run my little program sample above and make sure it actually did what I was saying it did. I just modified it to put lots of spaces between the individual words and also at the end of the string. After I reran the test program I got the same results... so it would seem you can use the example provided as is with no changes and it will still do what you want.
-
thanks a lot...ill get on it right away...
do you know what's the proper way to copy the string from word to a static char array...
this doesn't work for me:
Code:
static char classes[MAX_CLASSES][MAX_STRING];
classes[0] = word; //error on this line
error is: E2277 Lvalue required
should i use strcpy?
it works if i just do: in >> classes[0];
btw word is defined as: char word[MAX_STRING];
Regards,
Farooq
-
ya i got it...i just use strcpy()
thanks...
Farooq
-
another thing...what if the string had commas in between them? like
Code:
std::string word = "*CLASS*,alt,hello,right ";
Regards,
Farooq
-
i just checked it and it doesn't work for commas...treats them like one word...there's another line in my file in which words are only separated only by commas...i have that working using getline(word, MAX_STRING, ',') and reading in word by word...but the problem is when i want to check the end of the line...right now i do this:
Code:
while (in.peek() != '\n')
{
getline(word,MAX_STRING, ',')
//process it
}
this is not too good because the file might contain at the end of a line a space and then a newline character...
so what's the best way to check that...is there anyway to modify the parser so it can have the commas as the delimiter?
Regards,
Farooq
-
You can use the replace function to change all commas to spaces and just run the same algorithm:
Code:
#include <sstream>
#include <string>
#include <algorithm>
...
std::string word = "*CLASS*,alt,hello,right ";
std::string temp;
std::replace(word.begin(),word.end(),',',' ');
std::istringstream parser(word);
while( parser >> temp )
std::cout << temp << std::endl;
-