473,508 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting a substring


Given a std::string of the form "default(N)" where N is an integer number of
any length (e.g. the literal string might be "default(243)"), what is the
quickest way to extract the characters representing the integer into another
std::string? In the example above, I'd want to end up with a std:string
whose value is "243".

The substrings "default(" and ")" are invariant - they're always present in
the string I have to work with. Furthermore, it is guaranteed that there
will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've
used them here are subjective measures, but I'm sure people will intuitively
get the idea of what I'm trying to accomplish.)
Jul 19 '05 #1
7 14802

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the
quickest way to extract the characters representing the integer into another std::string? In the example above, I'd want to end up with a std:string
whose value is "243".

The substrings "default(" and ")" are invariant - they're always present in the string I have to work with. Furthermore, it is guaranteed that there
will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've
used them here are subjective measures, but I'm sure people will intuitively get the idea of what I'm trying to accomplish.)


One thing missing from the original problem statement is that there can be
any number of characters after the closing parentheis. These characters can
be anything (including digits). Sorry.
Jul 19 '05 #2

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

Given a std::string of the form "default(N)" where N is an integer number
of
any length (e.g. the literal string might be "default(243)"), what is
the quickest way to extract the characters representing the integer into

another
std::string? In the example above, I'd want to end up with a std:string
whose value is "243".

The substrings "default(" and ")" are invariant - they're always present

in
the string I have to work with. Furthermore, it is guaranteed that there will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've used them here are subjective measures, but I'm sure people will

intuitively
get the idea of what I'm trying to accomplish.)


One thing missing from the original problem statement is that there can be
any number of characters after the closing parentheis. These characters

can be anything (including digits). Sorry.


Well, to try and get the discussion started, here's the best I've come up
with:

Assume the original string is in str.

string new_str(str.begin()+8, str.begin()+str.find(')'));

Can anybody suggest an improvement on this?
Jul 19 '05 #3

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

Given a std::string of the form "default(N)" where N is an integer number
of
any length (e.g. the literal string might be "default(243)"), what is
the quickest way to extract the characters representing the integer into

another
std::string? In the example above, I'd want to end up with a std:string
whose value is "243".

The substrings "default(" and ")" are invariant - they're always present

in
the string I have to work with. Furthermore, it is guaranteed that there will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've used them here are subjective measures, but I'm sure people will

intuitively
get the idea of what I'm trying to accomplish.)


One thing missing from the original problem statement is that there can be
any number of characters after the closing parentheis. These characters

can be anything (including digits). Sorry.

// between():
//
// Returns string found between first occurence
// of 'ldelim' and first occurence of 'rdelim'
//
// If either delimiter not found, returns empty string
std::string between(const std::string& s,
char ldelim, char rdelim)
{
static std::string::const_iterator b(s.begin());
static std::string::const_iterator e(s.end());
static std::string::const_iterator lp;
static std::string::const_iterator rp;

std::string result;

if((lp = std::find(b, e, ldelim)) != e)
if((rp = std::find(++lp, e, rdelim)) != e)
result = std::string(lp, rp);

return result;

}

int main()
{
const char LD('(');
const char RD(')');

std::string s("default(123)abc");
std::string b(between(s, LD, RD));
std::cout << b << '\n';
return 0;
}

Output:

123
Not thoroughly tested.

I'm not clear whether the presence the of string
"default" is required for validity of the found '('
character.

E.g. should

"abc(123)xyz" cause return of "123" or not?

The code above does.

HTH,
-Mike

Jul 19 '05 #4
> // between():
//
// Returns string found between first occurence
// of 'ldelim' and first occurence of 'rdelim'
//
// If either delimiter not found, returns empty string
std::string between(const std::string& s,
char ldelim, char rdelim)
{
static std::string::const_iterator b(s.begin());
static std::string::const_iterator e(s.end());
static std::string::const_iterator lp;
static std::string::const_iterator rp;

std::string result;

if((lp = std::find(b, e, ldelim)) != e)
if((rp = std::find(++lp, e, rdelim)) != e)
result = std::string(lp, rp);

return result;

}

int main()
{
const char LD('(');
const char RD(')');

std::string s("default(123)abc");
std::string b(between(s, LD, RD));
std::cout << b << '\n';
return 0;
}

Output:

123
Not thoroughly tested.

I'm not clear whether the presence the of string
"default" is required for validity of the found '('
character.

E.g. should

"abc(123)xyz" cause return of "123" or not?

The code above does.


Thanks Mike, a good general solution! To answer your question, the leading
prefix is indeed always precisely "default(".
Jul 19 '05 #5

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...
// between():
//
// Returns string found between first occurence
// of 'ldelim' and first occurence of 'rdelim'
//
// If either delimiter not found, returns empty string
std::string between(const std::string& s,
char ldelim, char rdelim)
{
static std::string::const_iterator b(s.begin());
static std::string::const_iterator e(s.end());
static std::string::const_iterator lp;
static std::string::const_iterator rp;

std::string result;

if((lp = std::find(b, e, ldelim)) != e)
if((rp = std::find(++lp, e, rdelim)) != e)
result = std::string(lp, rp);

return result;

}

int main()
{
const char LD('(');
const char RD(')');

std::string s("default(123)abc");
std::string b(between(s, LD, RD));
std::cout << b << '\n';
return 0;
}

Output:

123
Not thoroughly tested.

I'm not clear whether the presence the of string
"default" is required for validity of the found '('
character.

E.g. should

"abc(123)xyz" cause return of "123" or not?

The code above does.
Thanks Mike, a good general solution! To answer your question, the

leading prefix is indeed always precisely "default(".


Well, what I was asking is if there could be any strings
(e.g. due to invalid input) with an integer between parentheses
like that, that are *not* preceded by "default", and if so,
should they be rejected, or return the integer?

If you feel you've got what you need, never mind, I'm
just trying to point out that many times we don't take
all possible 'bad' inputs into account. :-)

-Mike
Jul 19 '05 #6
In article <vp************@news.supernews.com>, cp*@weenie.com says...

Given a std::string of the form "default(N)" where N is an integer number of
any length (e.g. the literal string might be "default(243)"), what is the
quickest way to extract the characters representing the integer into another
std::string? In the example above, I'd want to end up with a std:string
whose value is "243".
The easiest way would probably be using more or less anacrhonistic code:

char output[256]
sscanf(input.c_str(), "default(%255[0123456789])", output);
std::string real_output(output);

I don't see any way to get anything that neat and clean using code that
most people would think of as being "real" C++, but I guess something
like this should work:

#include <string>
#include <iostream>

using std::string;

string get_default(string input, string open, string close) {
std::string::size_type start = input.find(open)+open.length();
std::string::size_type end = input.find(close, start);

return std::string(input, start, end-start);
}

used something like this:

std::string test_input("(bad output) default(1234567)a2w");

std::string default = get_default(test_input, "default(", ")");

If you're sure "default(" and ")" are truly constant and you'll never
want to use other delimiters, you could eliminate them as parameters:

std::string get_default(std::string input) {
std::string open("default(");
std::string close(")");

std::string::size_type start = input.find(open)+open.length();
std::string::size_type end = input.find(close, start);

return std::string(input, start, end-start);
}

in which case you'd use it like:

std::string default = get_default(test_input);
The substrings "default(" and ")" are invariant - they're always present in
the string I have to work with. Furthermore, it is guaranteed that there
will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've
used them here are subjective measures, but I'm sure people will intuitively
get the idea of what I'm trying to accomplish.)


You could certainly get shorter than these, but I doubt you could do so
to any substantial degree (then again, they're already short enough that
there really IS not substantial degree of "shorter").

Speed depends: if you expect to scan across a LOT of text looking for
"default(", (e.g. there might be 100K of garbage before "default(" )
then there are various advanced string searching algorithms that would
probably help performance (theoretically, std::string::find could
already use them, but I've yet to see that done). I doubt that applies
here though, so I won't try to go into more detail, at least for now.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7
"cpp_weenie" <cp*@weenie.com> wrote in message news:<vp************@news.supernews.com>...
"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

"cpp_weenie" <cp*@weenie.com> wrote in message
news:vp************@news.supernews.com...

Given a std::string of the form "default(N)" where N is an integer number
of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters representing the integer into another std::string? In the example above, I'd want to end up with a std:string
whose value is "243".

The substrings "default(" and ")" are invariant - they're always present in the string I have to work with. Furthermore, it is guaranteed that there will be at least one numeric character between the open and close
parentheses, and it is also guaranteed that there will be nothing but
numeric characters between the open and close parentheses.

I'm looking for the shortest possible standard-compliant and safe way of
making this substring extraction. (Yes, "shortest" and "quickest" as I've used them here are subjective measures, but I'm sure people will intuitively get the idea of what I'm trying to accomplish.)


One thing missing from the original problem statement is that there can be
any number of characters after the closing parentheis. These characters

can
be anything (including digits). Sorry.


Well, to try and get the discussion started, here's the best I've come up
with:

Assume the original string is in str.

string new_str(str.begin()+8, str.begin()+str.find(')'));

Can anybody suggest an improvement on this?


Yes:

string x ("default(123)xyz");
string digits (x, 8, x.find (')', 9) - 8);

This saves 9 comparisons in the find() compared to your method :)

Also I have a feeling that iterators and c_str() are potentially less
efficient than a direct constructor call (if say, the underlying
string buffer is not contiguous).

Davlet.
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1364
by: Abby Lee | last post by:
I have a string with 10 charaters 123456789A. I only want the first 6 charaters followed by the last two...getting rid of the 7th and 8th charaters. If I use document.forms.MyTextBox.value =...
3
2643
by: War Eagle | last post by:
I've been looking at .substring and .trim methods and I still have a question about extracting substrings from a textbox. Basically the textbox contains the full path to a file ... for example ...
6
1834
by: RSH | last post by:
Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk...
0
1149
by: Ankit Aneja | last post by:
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); MessageBox.Show(path); it...
1
7376
by: RogueClient | last post by:
Hi all, I have what I suspect is a day one rookie question yet for the life of me I can't find a good answer on the net. I need to extract a quoted substring from within a string like so: ...
1
1388
by: annaannie | last post by:
hello sir, My aim is to extract 'id' and 'ac' from given XML files,and store the results in two different files.the code i wrote can extract 'ids',and give the output in a file.But i cant extract...
1
1584
by: Phat G5 (G3) | last post by:
I found this little script for extracting parameters from a url but wondered what the shortest and most efficient way to do it would be, like the following or via regexp? function...
0
3534
by: pankajd | last post by:
hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that...
11
4528
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.