Solved

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

Posted on 2003-11-26
14
371 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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