-1

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?

user5991813
  • 59
  • 1
  • 2
  • 7

3 Answers3

1

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...

DrDonut
  • 864
  • 14
  • 26
0
#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;
}
Y.W
  • 1
  • 1
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 ?.

XAMPPRocky
  • 3,160
  • 5
  • 25
  • 45
Mayur
  • 1