how to get string from a line of strings? - c++

I have following string:
"hw_core_detectionhook::Iocard const*"
I have to get only first part, i.e all text present before space, i.e I need the "hw_core_detectionhook::Iocard" part only.

std::stringstream ss;
ss << "hw_core_detectionhook::Iocard const*";
std::string s;
ss >> s;
std::cout << s;
Output:
hw_core_detectionhook::Iocard
See the complete demo online : http://www.ideone.com/w9l1C

s.substr(0,s.find_first_of(" "));

Related

Split string with space but only get first word

I'm having problem to split the string with space as a delim. I have tried 2 of the proposed solution as in here:
Split a string in C++?
(using copy + istringstream and split method)
However, no matter what I did, the vector only get the first word (not the rest). When I use the split method, it's working with anything else (dot, comma, semi colon...) but not space.
Here is my current code, can you tell me what I get wrong? Or how I should try to approach the fix?
int main()
{
std::vector<std::string> textVector;
std::string textString;
std::cout << "Input command : ";
std::cin >> textString;
std::istringstream iss(textString);
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter(textVector));
for (int i = 0 ; i < textVector.size(); i++) {
std::cout << textVector[i];
}
return 0;
}
The runnable code: http://cpp.sh/8nzq
Reason is simple, std::cin >> textString only reads until first whitespace. So textString only contains the first word.
To read entire line, you should instead use: std::getline(std::cin, textString);

White space insertion in std::stringstream fails

I'm trying to insert white space in std::stringstream in this way.
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
and then setting it as a label like that
label->setString(sstr.str().c_str());
but it's only giving me 10, space is not included. I've followed many links to solve problem but of no use. Following link suggests to use getline() but in my case I cannot do that :
stringstream doesn't accept white space?
I've also tried to use std::noskipws but it also not work :
sstr << std::noskipws << " ";
Any help will be appreciated.
std::stringstream should not remove your whitespace when used like this. Are you sure that it is not the label-object that is trimming your string and removing the whitespace?
Try debugging or printing out your string before setting it to the label.
You can use put method to append a single char:
std::stringstream sstr;
sstr.str("");
sstr.put(' ');
sstr.put(10);
I think you're using it wrong:
string labelstr;
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
ss >> noskipws >> labelstr;
label->setString(labelstr);
http://www.cplusplus.com/reference/ios/noskipws/
Your code works as expected for me. Here's a runnable example: http://cpp.sh/8d6.
The culprit must be setString trimming your input. Or possibly some other function that reads the string later.

std::stringstream only supports one input at a time?

Since std::stringstream is a stzream, and according to the documention here, you can perform any operation a stream supports.
So I expected the following sample to work, but it seems it doesn't. I'm using MingW with gcc 4.8.3.
Variant A:
std::string s;
std::stringstream doc;
doc << "Test " << "String ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
Variant B:
std::string s;
std::stringstream doc;
doc << "Test ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
The output of this is only
Test
While I expected that it would concatenate the individual strings until I read from the stream back what I put there.
So what is the approperiate way to concatenate strings? Do I really have to read out each one individually and concatenate them manually, which seems quite awkward to me in C++.
It is putting each of the strings into doc, so that its content is:
Test String AnotherString
Then when you extract using doc >> s, it only reads up to the first whitespace. If you want to get the entire stream as a string, you can call str:
std::cout << doc.str() << std::endl;
It will only read one word till a white-space by using stream >> s. Besides #JosephMansfield's answer of using str(), alternatively you can use getline() (works perfectly if you the string doesn't contains new lines):
getline(doc, s);

Getting the remainder of a stringstream c++

I have a stringstream where I need to get the first part out and then get the remainder into a separate string. For example I have the string "This is a car" and I need to end up with 2 strings: a = "This" and b = "is a car".
When I use stringstream to get the first part using <<,then I use the .str() to convert to a string which of course gave me the whole thing "This is a car". How can I get it to play how I want?
string str = "this is a car";
std::stringstream ss;
ss << str;
string a,b;
ss >> a;
getline(ss, b);
EDIT: correction thanks to #Cubbi:
ss >> a >> ws;
EDIT:
This solution can handle newlines in some cases (such as my test cases) but fails in others (such as #rubenvb's example), and I haven't found a clean way to fix it. I think #tacp's solution is better, more robust, and should be accepted.
You can do this: first get the whole string, then get the first word, using substr to get the rest.
stringstream s("This is a car");
string s1 = s.str();
string first;
string second;
s >> first;
second = s1.substr(first.length());
cout << "first part: " << first <<"\ second part: " << second <<endl;
Testing this in gcc 4.5.3 outputs:
first part: This
second part: is a car
You can do a getline on the stream after reading out the first bit....
Another way to do this is with rdbuf:
stringstream s("This is a car");
string first;
stringstream second;
s >> first;
second << s.rdbuf();
cout << "first part: " << first << " second part: " << second.str() << endl;
This may be a good option if you're ultimately going to output the result to a stream instead of a string.

Substring new line

I am using the substr() function, however it's not working. My code is below
std::string s1 = ".V/123\n"
".V/233\n";
std::string ss;
if(s1.substr(0,3) == ".V/")
{
ss = s1.substr(3);
std::cout << ss;
} else {
std::cout << "INCORRECT" << std::endl;
}
The output is 123.V/123
Shouldn't it be:
123
123
Could someone tell me where I am going wrong please?
Your string contains 2 lines. Doing ".V/123\n" ".V/223\n" will do the same as ".V/123\n.V/223\n". Either split that up into separate variables or an array. What your substr(3) is doing is extracting all the characters in the string from the 4th character to the end, so 'ss' is getting set to "123\n.V/223\n". This is what you're seeing.
What you want is something like this
std::string s1[2] = { ".V/123\n", ".V/233\n"};
std::string ss;
for (int i = 0; i < 2; ++i) {
if (s1[i].substr(0,3) == ".V/") {
ss = s1[i].substr(3);
std::cout << ss;
} else {
std::cout << "INCORRECT" << std::endl;
}
}
The output from your code should be:
123
.V/233
<empty line>
and this is exactly what it prints on my machine.
First of all notice the second 3 digits are 233(I guess that is a typo), not 123. All substr(3) does is: it removes the first 3 characters from your string. No reason that .V/ should be removed.
The first strange thing I see is the init of s1
std::string s1 = ".V/123\n"
".V/233\n";
if you just want it to be ".V/123\n", you should declare it as:
std::string s1 = ".V/123\n";
The second strange thing is the second call to substr, only defines the position, so you will get from the position to the end of the string, so that works as advertised. I added a reference for you below.
reference:
http://www.cplusplus.com/reference/string/string/substr/
ss = s1.substr(3).substr(0,3);
this should work