Solved

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

Posted on 2003-11-26
14
356 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

896 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

11 Experts available now in Live!

Get 1:1 Help Now