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
Related
The text to the left and right of the integer is relevant to initialize an object, so it needs to be kept. The starting of the integer will persist and i'm thinking I will try and use a while loop to check to see if the next character is also digit, but I feel like their must be a more elegant solution. Does anyone have any hints, tips, or tricks?
Instead of writing a while loop to convert a sequence of charachers to an integer value, the standard library provides std::istringstream and formatted input (operator>>() as illustrated by this simple example:
void example1()
{
std::string s{"Hello 1234 World\n"};
std::istringstream ss(s.substr(6,std::string::npos));
int nbr;
ss >> nbr;
std::cout << "The number is " << nbr << std::endl;
}
In this example, the starting position of the string is known. If not, you would need to either parse the string (which in simple cases can be done using similar techniques). For instance, if the number is preceeded by the string "Nbr:", you can use string::find
void example2()
{
std::string s{"Some uninteresting text... Nbr: 1234 World\n"};
auto pos = s.find("Nbr:");
std::istringstream ss(s.substr(pos+4,std::string::npos));
int nbr;
ss >> nbr;
std::cout << "The number is " << nbr << std::endl;
}
Or you can used regex to find the first number in the string and use std::stoi on the submatch:
void example3()
{
std::string s{"Some uninteresting text... : 1234 World\n"};
std::regex rgx("[^0-9]*(\\d+).*");
std::smatch match;
if (std::regex_search(s, match, rgx)) {
auto n = std::stoi(match[1]);
std::cout << "the number is " << n << '\n';
} else {
std::cout << "no match\n";
}
}
I would like to check the following:
If the last character appended to the stringstream is a comma.
If it is remove it.
std::stringstream str;
str << "["
//loop which adds several strings separated by commas
str.seekp(-1, str.cur); // this is to remove the last comma before closing bracket
str<< "]";
The problem is if nothing is added in the loop, the opening bracket is removed from the string. So I need a way to check whether the last character is a comma. I did that like this:
if (str.str().substr(str.str().length() - 1) == ",")
{
str.seekp(-1, rteStr.cur);
}
But I don't feel very good about this. Is there a better way to do this?
About the loop:
Loop is used to tokenize a set of commands received through sockets and format it to send to another program through another socket. Each command ends with an OVER flag.
std::regex tok_pat("[^\\[\\\",\\]]+");
std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
std::sregex_token_iterator tok_end;
std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);
while (baseStr == "OVER")
{
//extract command parameters
str << "extracted_parameters" << ","
}
The way I often deal with these loops where you want to put something like a space or a comma between a list of items is like this:
int main()
{
// initially the separator is empty
auto sep = "";
for(int i = 0; i < 5; ++i)
{
std::cout << sep << i;
sep = ", "; // make the separator a comma after first item
}
}
Output:
0, 1, 2, 3, 4
If you want to make it more speed efficient you can output the first item using an if() before entering the loop to output the rest of the items like this:
int main()
{
int n;
std::cin >> n;
int i = 0;
if(i < n) // check for no output
std::cout << i;
for(++i; i < n; ++i) // rest of the output (if any)
std::cout << ", " << i; // separate these
}
In your situation the first solution could work like this:
std::regex tok_pat("[^\\[\\\",\\]]+");
std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
std::sregex_token_iterator tok_end;
std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);
auto sep = ""; // empty separator for first item
while (baseStr == "OVER")
{
// extract command parameters
str << sep << "extracted_parameters";
sep = ","; // make it a comma after first item
}
And the second (possibly more time efficient) solution:
std::regex tok_pat("[^\\[\\\",\\]]+");
std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
std::sregex_token_iterator tok_end;
std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);
if (baseStr == "OVER")
{
// extract command parameters
str << "extracted_parameters";
}
while (baseStr == "OVER")
{
// extract command parameters
str << "," << "extracted_parameters"; // add a comma after first item
}
Let's say I have
string sentence{"Hello how are you."}
And I want string sentence to have "how are you" without the "Hello". How would I go about doing that.
I tried doing something like:
stringstream ss(sentence);
ss>> string junkWord;//to get rid of first word
But when I did:
cout<<sentence;//still prints out "Hello how are you"
It's pretty obvious that the stringstream doesn't change the actual string. I also tried using strtok but it doesn't work well with string.
Try the following
#include <iostream>
#include <string>
int main()
{
std::string sentence{"Hello how are you."};
std::string::size_type n = 0;
n = sentence.find_first_not_of( " \t", n );
n = sentence.find_first_of( " \t", n );
sentence.erase( 0, sentence.find_first_not_of( " \t", n ) );
std::cout << '\"' << sentence << "\"\n";
return 0;
}
The output is
"how are you."
str=str.substr(str.find_first_of(" \t")+1);
Tested:
string sentence="Hello how are you.";
cout<<"Before:"<<sentence<<endl;
sentence=sentence.substr(sentence.find_first_of(" \t")+1);
cout<<"After:"<<sentence<<endl;
Execution:
> ./a.out
Before:Hello how are you.
After:how are you.
Assumption is the line does not start with an empty space. In such a case this does not work.
find_first_of("<list of characters>").
the list of characters in our case is space and a tab. This will search for first occurance of any of the list of characters and return an iterator. After that adding +1 movers the position by one character.Then the position points to the second word of the line.
Substr(pos) will fetch the substring starting from position till the last character of the string.
You can for example take the remaining substring
string sentence{"Hello how are you."};
stringstream ss{sentence};
string junkWord;
ss >> junkWord;
cout<<sentence.substr(junkWord.length()+1); //string::substr
However, it also depends what you want to do further
There are countless ways to do this. I think I would go with this:
#include <iostream>
#include <string>
int main() {
std::string sentence{"Hello how are you."};
// First, find the index for the first space:
auto first_space = sentence.find(' ');
// The part of the string we want to keep
// starts at the index after the space:
auto second_word = first_space + 1;
// If you want to write it out directly, write the part of the string
// that starts at the second word and lasts until the end of the string:
std::cout.write(
sentence.data() + second_word, sentence.length() - second_word);
std::cout << std::endl;
// Or, if you want a string object, make a copy from the start of the
// second word. substr copies until the end of the string when you give
// it only one argument, like here:
std::string rest{sentence.substr(second_word)};
std::cout << rest << std::endl;
}
Of course, unless you have a really good reason not to, you should check that first_space != std::string::npos, which would mean the space was not found. The check is omitted in my sample code for clarity :)
You could use string::find() to locate the first space. Once you have its index, then get the sub string with string::substr() from the index after the index of the space up to the end of the string.
One liner:
std::string subStr = sentence.substr(sentence.find_first_not_of(" \t\r\n", sentence.find_first_of(" \t\r\n", sentence.find_first_not_of(" \t\r\n"))));
working example:
#include <iostream>
#include <string>
void main()
{
std::string sentence{ "Hello how are you." };
char whiteSpaces[] = " \t\r\n";
std::string subStr = sentence.substr(sentence.find_first_not_of(whiteSpaces, sentence.find_first_of(whiteSpaces, sentence.find_first_not_of(whiteSpaces))));
std::cout << subStr;
std::cin.ignore();
}
Here's how to use a stringstream to extract the junkword while ignoring any space before or after (using std::ws), then get the rest of the sentence, with robust error handling....
std::string sentence{"Hello how are you."};
std::stringstream ss{sentence};
std::string junkWord;
if (ss >> junkWord >> std::ws && std::getline(ss, sentence, '\0'))
std::cout << sentence << '\n';
else
std::cerr << "the sentence didn't contain ANY words at all\n";
See it running on ideone here....
#include <iostream> // cout
#include <string> // string
#include <sstream> // string stream
using namespace std;
int main()
{
string testString = "Hello how are you.";
istringstream iss(testString); // note istringstream NOT sstringstream
char c; // this will read the delima (space in this case)
string firstWord;
iss>>firstWord>>c; // read the first word and end after the first ' '
cout << "The first word in \"" << testString << "\" is \"" << firstWord << "\""<<endl;
cout << "The rest of the words is \"" <<testString.substr(firstWord.length()+1) << "\""<<endl;
return 0;
}
output
The first word in "Hello how are you." is "Hello"
The rest of the words is "how are you."
live testing at ideon
I've been looking thousand of questions and answers about what I'm going to ask, but I still didn't find the way to do what I'm gonna to explain.
I have a text file from which I have to extract information about several things, all of them with the following format:
"string1":"string2"
And after that, there is more information, I mean:
The text file is something like this:
LINE 1
XXXXXXXXXXXXXXXXXXXXXXXXXXXX"string1":"string2"XXXXXXXXXXXXXXXXXXXXXXXXXX"string3":"string4"XXXXXXXXXXXXXXXXXXXXXXXXXXXX...('\n')
LINE 2
XXXXXXXXXXXXXXXXXXXXXXXXXXXX"string5":"string6"XXXXXXXXXXXXXXXXXXXXXXXXXX"string7":"string8"XXXXXXXXXXXXXXXXXXXXXXXXXXXX...
XXX represents irrelevant information I do not need, and theEntireString (string used in the code example) stores all the information of a single line, not all the information of the text file.
I have to find first the content of string1 and store the content of string2 into another string without the quotes. The problem is that I have to stop when I reache the last quote and I don't know how exactly do this. I suppose I have to use the functions find() and substr(), but despite having tried it repeatedly, I did not succeed.
What I have done is something like this:
string extractInformation(string theEntireString)
{
string s = "\"string1\":\"";
string result = theEntireString.find(s);
return result;
}
But this way I suppose I store into the string the last quote and the rest of the string.
"find" function just give you the position of matched string to get the resulting string you need to use the "subst" function. Try This
string start,end;
start = theEntireString.substr(1,theEntireString.find(":")-2);
end = theEntireString.substr(theEntireString.find(":")+2,theEntireString.size()-1);
That will solve you problem
Assuming either the key or value contains a quotation mark. The following will output the value after the ":". You can also use it in a loop to repeatedly extract the value field if you have multiple key-value pairs in the input string, provided that you keep a record of the position of last found instance.
#include <iostream>
using namespace std;
string extractInformation(size_t p, string key, const string& theEntireString)
{
string s = "\"" + key +"\":\"";
auto p1 = theEntireString.find(s);
if (string::npos != p1)
p1 += s.size();
auto p2 = theEntireString.find_first_of('\"',p1);
if (string::npos != p2)
return theEntireString.substr(p1,p2-p1);
return "";
}
int main() {
string data = "\"key\":\"val\" \"key1\":\"val1\"";
string res = extractInformation(0,"key",data);
string res1 = extractInformation(0,"key1",data);
cout << res << "," << res1 << endl;
}
Outputs:
val,val1
Two steps:
First we have to find the position of the : and splice the string into two parts:
string first = theEntireString.substr(0, theEntireString.find(":"));
string second = theEntireString.substr(theEntireString.find(":") + 1);
Now, we have to remove the "":
string final_first(first.begin() + 1, first.end() - 1);
string final_second(second.begin() + 1, second.end() - 1);
You don't need any string operation. I hope the XXXXX doesn't contain any '"', so You can read the both strings directly from the file:
ifstream file("input.txt");
for( string s1,s2; getline( getline( file.ignore( numeric_limits< streamsize >::max(), '"' ), s1, '"' ) >> Char<':'> >> Char<'"'>, s2, '"' ); )
cout << "S1=" << s1 << " S2=" << s2 << endl;
the little help-function Char is:
template< char C >
std::istream& Char( std::istream& in )
{
char c;
if( in >> c && c != C )
in.setstate( std::ios_base::failbit );
return in;
}
#include <regex>
#include <iostream>
using namespace std;
const string text = R"(
XXXXXXXXXXXXXXXXXXXXXXXXXXXX"string1":"string2"XXXXXXXXXXXXXXXXXXXXXXXXXX"string3" :"string4" XXXXXXXXXXXXXXXXXXXXXXXXXXXX...
XXXXXXXXXXXXXXXXXXXXXXXXXXXX"string5": "string6"XXXXXXXXXXXXXXXXXXXXXXXXXX"string7" : "string8" XXXXXXXXXXXXXXXXXXXXXXXXXXXX...
)";
int main() {
const regex pattern{R"~("([^"]*)"\s*:\s*"([^"]*)")~"};
for (auto it = sregex_iterator(begin(text), end(text), pattern); it != sregex_iterator(); ++it) {
cout << it->format("First: $1, Second: $2") << endl;
}
}
Output:
First: string1, Second: string2
First: string3, Second: string4
First: string5, Second: string6
First: string7, Second: string8
Running (with clang and libc++): http://coliru.stacked-crooked.com/a/f0b5fd383bc227fc
This is how raw string literals look in an editor that understand them: http://bl.ocks.org/anonymous/raw/9442865/
I'm running into a problem when reading from a file. Here is some sample code:
std::for_each(vec.begin(), vec.end(), [&](std::string str1) {
//... split the string up by spaces into vector "split"
for (auto& str : split) {
std::cout << str << "\n";
std::cout << str[0] << "\n";
}
});
So basically I print each element of split out on one line, and then print the first character. What I get is this:
“test test test
?
where the first line is the whole str, and the second line should be the first character. However, it prints a ? instead. Even using a std::string will give the same result:
for (auto& str : split) {
std::cout << str << "\n";
std::cout << std::string(1, str[0]) << "\n";
}
I'm using clang++ -std=c++11 as my setup. Has anyone else seen this?
Your string is probably not encoded one element => one character. I was able to reproduce your issue here and it looks like the special quotation mark takes the first three string bytes, so this would print it properly:
std::cout << s.substr(0, 3);
Your situation could be a bit different, but I'm guessing it's UTF-8 for both you and Coliru. Specifically, the character looks to be this one, the "left double quotation mark", with a UTF-8 value of 0xE2809C.