This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string
thanks
#include <sstream>
#include <vector>
#include <string>
int main() {
std::string str("abc\tdef");
char split_char = '\t';
std::istringstream split(str);
std::vector<std::string> token;
for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}
Why can't you use C standard library?
Variant 1.
Use std::string::c_str() function to convert a std::string to a C-string (char *)
Variant 2.
Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.
Related
This question already has answers here:
Parsing a comma-delimited std::string [duplicate]
(18 answers)
Closed 3 years ago.
I have a string that contains 3 numbers separated by commas (for example "10,32,52") and I would like to store each number in 3 different int variables, but I just know how to store the first one using the code below. Can you tell me please how can I store the next two?
Thanks in advance.
string numbers= "10,32,52";
string first_number_s= numbers.substr(0,2);
int first_number_n= stoi(first_number_s);
You could stream the input string into a string stream and use std::getline to extract strings separated by a delimiter, in this case comma, converting each one to an integer:
#include <sstream>
#include <string>
std::string numbers = "10,32,52";
std::istringstream sstream(numbers);
std::string temp;
std::vector<int> nums;
while (std::getline(sstream, temp, ','))
{
nums.push_back(std::stoi(temp));
}
This question already has answers here:
C++: Converting a double/float to string, preserve scientific notation and precision
(2 answers)
Closed 5 years ago.
This question is very clearly in no way an "exact duplicate" of the marked question, and that post does not address my goal.
If I have:
double yuge = 1e16;
And I try to add it to a string like this:
std::string boogers = std::to_string (yuge) + ".csv";
I get a file name of 100000000000000000.csv.
I want a nice compact version like 1e16.csv.
As you can see, I would like to use it as a file name, so output methods arent helpful. Halp! Thanks.
you can use std::stringstream to construct the string instead of + operator.
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
int main()
{
double youge = 1e16;
std::stringstream ss;
ss<<youge<<".csv";
std::string filename = ss.str(); // filename = 1e+16.csv
filename.erase(std::remove(filename.begin(), filename.end(), '+'), filename.end());// removing the '+' sign
std::cout<<filename; // 1e16.csv
}
This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 9 years ago.
I have a string and I want to split it every time the char ',' appears.
I want to save the result in a vector of pointers to string.
What is the best way to do this?
"i want to split it every time that the char ',' ..."
Use std::getline and specify the delimiter (last argument) to be ','.
"I want to save the result in a vector of pointers to string"
You want to avoid using vector of pointers, believe me. Use std::vector<std::string> instead:
std::istringstream is(",,,my,,weird,string");
std::vector<std::string> tokens;
std::string token;
while (std::getline(is, token, ',')) {
if (!token.empty())
tokens.push_back(token);
}
for (int i = 0; i < tokens.size(); ++i)
std::cout << tokens[i] << " ";
outputs my weird string. Just don't forget to #include <sstream>.
boost::algorithm::split
Or write your own. This algorithm is pretty easy to write in terms of std::find.
I've used strtok to tokenize string, but this has a few drawbacks:
This is part of cstring and it's used for C-style strings, and not std::string objects.
It's kind of clumsy in terms of having to call it several times changing the parameter after the first time.
It's not ideal if you have boost available but it should work for all implementations of C++.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
pseudocode:
Attributes[] = Split line(' ')
How?
I have been doing this:
char *pch;
pch = strtok(line," ");
while(pch!=NULL)
{
fputs ( pch, stdout );
}
and getting a non-written, stuck, exit file. It's something wrong with this?
Well, the thing isn't even meeting my pseudocode requirement, but I'm confused about how to index tokens (as char arrays) to my array, I guess I should write a 2-dim array?
Use strtok with " " as your delimiter.
This is not quite a dup - for C++ see and upvote the accepted answer here by #Zunino.
Basic code below but to see the full glorious elegance of the answer you are going to have to click on it.
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
int main() {
using namespace std;
string sentence = "Something in the way she moves...";
istringstream iss(sentence);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));
}
This hinges on the fact that by default, istream_iterator treats whitespace as its separator. The resulting tokens are written to cout on separate lines (per separator specified in constructor overload for ostream_iterator).
The easiest method is boost::split:
std::vector<std::string> words;
boost::split(words, your_string, boost::is_space());