• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

How can I scan out the sub-string in a string

Assumed here is a string as follow:

 <INPUT TYPE="password" NAME="password" ID="password"  SIZE="20"  MAXLENGTH="20" >

I just know the reserved words such as NAME and ID. What I have to do now is to scan out the value after the reserved words such as "password" after NAME and "password" after ID.
0
chenwei
Asked:
chenwei
  • 7
  • 7
1 Solution
 
SteHCommented:
int pos = searchtring.find ("NAME"); // returns the position where NAME is inside the string.
string afterName = searchstring.substr (pos+5); // is the values thereafter.
0
 
chenweiAuthor Commented:
add:

The string given above is gained in run time. That means one doesn't know how is looks like before.
0
 
SteHCommented:
in the case "NAME" is not part of searchstring string::npos is returned from find.
 int pos = searchtring.find ("NAME");
 if (pos != string::npos)
     string afterName = searchstring.substr (pos+5);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chenweiAuthor Commented:
what a class is searchstring? I couldn't find it in Visual.NET.
0
 
SteHCommented:
sorry I forgot the declarations:

#include <string>

string searchstring;
...
0
 
chenweiAuthor Commented:
Ok, I got it. It's a memeber of basic_string.

I've tryed and it runs. But it seems a little bit complicated. I wonder if I can scan out the value of NAME and ID at a once. I wonder if I can use sscanf() to do that.
0
 
SteHCommented:
sscanf needs a preknown format. For a variable string this is very difficult or impossible unless you look for the position of NAME and ID beforehand. Whether this is less complicated I don't know. Advantage of class string is you don't have to care about the length.
0
 
chenweiAuthor Commented:
The string is more or less preknown, since it contains always the key word such as "NAME", "ID" ect. I want to scan out the values of NAME and ID with a sentence such as sscanf().
0
 
SteHCommented:
But for sscanf you have to know at least everything before those strings and if you want to do it in one go you need to know how many strings are inbetween. sscanf takes some (fixed) format argument. If it is always of the form <INPUT TYPE="xxx" NAME="yyy" ID="zzz" ...> it will be easier:

char cBuf1[21], cBuf2[21], cBuf3[21]; // assuming all can only be 20 chars + null terminator.
sscanf ("<INPUT TYPE=\"%s\" NAME=\"%s\" ID=\"%s\", cBuf1, cBuf2, cBuf3); // not sure whether \"%s\" or just %s should be taken.

But this is C code not C++. And even the C++ will get easier if you can assume how the input string will look like.
0
 
chenweiAuthor Commented:
If you think C++ will get easier, could you please tell me with which function to do that?
0
 
SteHCommented:
sstream ss (<input>); // replace <input> with correct type/variable of your input.

string trash;
string typepasswd,namepasswd, idpasswd;
getline (ss, trash, '='); // remove all chars until = is found
ss >> typepasswd;      // "password" for TYPE
getline (ss, trash,'=');   // "forward after NAME=
ss >> namepasswd;
getline (ss, trash,'=');
ss >> idpasswd;
0
 
chenweiAuthor Commented:
Hi, I try the follwoing code:

...
#include <sstream>
...

  string s1;
  sstream ss("ny name");
 
  getline(ss, s1, ' ');
  cout <<  s1 << endl;
...

But i got error that ss and sstream not deklared.
0
 
SteHCommented:
It should be
stringstream ss ();
0
 
chenweiAuthor Commented:
Thank you!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now