I have the string String str = "my age is how old i am?how old are you?";
And I want to extract just the string "how old are you" and place it in another variable.
How can I do this?
I have the string String str = "my age is how old i am?how old are you?";
And I want to extract just the string "how old are you" and place it in another variable.
How can I do this?
You can use the substring method: http://www.cplusplus.com/reference/string/string/substr/
int first = str.find('?')
String ageStr = str.substr(first, str.find('?', first) - first);
This way you select the part of the string between the position of the first question mark, and the position of the second question mark...
#include <string>
#include <iostream>
using namespace std;
string GetStringInBetween(string Src,string FirstTag, string Secondag)
{
size_t FirstPos,SecondPos;
string newstr="";
FirstPos = Src.find(FirstTag);
if (FirstPos != string::npos)
{
FirstPos++;
SecondPos = Src.find(Secondag, FirstPos );
if (SecondPos != string::npos)
{
newstr = Src.substr(FirstPos , SecondPos - FirstPos);
}
}
return newstr;
}
int main()
{
string str = "?how old are you?";
string nstr = GetStringInBetween(str, "?", "?");
cout << nstr;
return 0;
}
for(int i=0;i<strlen(str);i++)
{
if(str[i] != '?')
{
cout<<str[i];
}
}
You can add any symbol of your choice in place of ?
.