Solved

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

Posted on 2003-11-26
14
346 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now