Solved

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

Posted on 2003-11-26
14
376 Views
Last Modified: 2010-04-01
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
Comment
Question by:chenwei
  • 7
  • 7
14 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9825340
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
 

Author Comment

by:chenwei
ID: 9825355
add:

The string given above is gained in run time. That means one doesn't know how is looks like before.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9825371
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
Technology Partners: 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!

 

Author Comment

by:chenwei
ID: 9825397
what a class is searchstring? I couldn't find it in Visual.NET.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9825408
sorry I forgot the declarations:

#include <string>

string searchstring;
...
0
 

Author Comment

by:chenwei
ID: 9825517
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
 
LVL 13

Expert Comment

by:SteH
ID: 9825657
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
 

Author Comment

by:chenwei
ID: 9830527
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
 
LVL 13

Expert Comment

by:SteH
ID: 9830633
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
 

Author Comment

by:chenwei
ID: 9831034
If you think C++ will get easier, could you please tell me with which function to do that?
0
 
LVL 13

Expert Comment

by:SteH
ID: 9831215
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
 

Author Comment

by:chenwei
ID: 9831700
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
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
ID: 9831788
It should be
stringstream ss ();
0
 

Author Comment

by:chenwei
ID: 9835622
Thank you!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question