473,803 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

capture substring in the most efficient

hi,

let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.
Nov 14 '05 #1
15 2022
"Magix" <ma***@asia.com > wrote in message
news:40******** **@news.tm.net. my...
let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ?
which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.


There's no other practical way to do it. You could use sscanf() but that
will just use a loop internally.

Alex
Nov 14 '05 #2
"Alex Fraser" <me@privacy.net > wrote:
"Magix" <ma***@asia.com > wrote in message
news:40******** **@news.tm.net. my...
let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ?
which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.
There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems if
the string can contain internal '"'s, but you'll have to devise a
solution for that case anyway.
You could use sscanf() but that will just use a loop internally.


So does strchr(), but the loop in such functions is likely to be better
optimised than a normal for loop.

Richard
Nov 14 '05 #3
Richard Bos wrote:
"Alex Fraser" <me@privacy.net > wrote:
"Magix" <ma***@asia.com > wrote in message
news:40******** **@news.tm.net. my...
> let say I have this string
>
> +CCED: "xxxxxxxxx" , 333
>
> What is the most efficient way to capture the string within the
> " " ? which is xxxxxxxxx
> xxxxxxxxx can be any string.
>
> For loop solutions is not the option.


There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems
if the string can contain internal '"'s, but you'll have to devise
a solution for that case anyway.
...

Good starting point. Let's do it this way:

Assuming that string is correct with respect to what you told us:
it really starts with +CC... and terminates with a ,<number>

char destination[maxLenOfString+ 1];
/* if maxLenOfString is unknown, use malloc() instead */

char * pstart = strchr(yourStri ng,'"'); /* points to first " */
char * pstop = strrchr(yourStr ing,'"'); /* points to last " */

size_t len = pstop-pstart; /* contains one " still */

strncpy (destination, pstart, len-1);
destination[len] = '\0';

( Code not checked )

Bernhard
Nov 14 '05 #4
Bernhard Holzmayer wrote:
Richard Bos wrote:

"Alex Fraser" <me@privacy.net > wrote:

"Magix" <ma***@asia.com > wrote in message
news:40***** *****@news.tm.n et.my...

let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the
" " ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems
if the string can contain internal '"'s, but you'll have to devise
a solution for that case anyway.
...


Good starting point. Let's do it this way:

Assuming that string is correct with respect to what you told us:
it really starts with +CC... and terminates with a ,<number>

char destination[maxLenOfString+ 1];
/* if maxLenOfString is unknown, use malloc() instead */

char * pstart = strchr(yourStri ng,'"'); /* points to first " */
char * pstop = strrchr(yourStr ing,'"'); /* points to last " */

size_t len = pstop-pstart; /* contains one " still */

strncpy (destination, pstart, len-1);
destination[len] = '\0';

( Code not checked )

Bernhard


So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.

One must use some kind of iteration to find the substring
unless:
1. The source string has fixed length fields.
or 2. The string is lexicographical ly order so that one
can use other search algorithms.

Perhaps, the OP should state why "for" loops must be avoided.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5
Thomas Matthews wrote:
So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.
I guessed that the OP didn't want to code explicit for loops,
because of poor performance, but that (hopefully optimized)
functions like strchr() would be acceptable.

One must use some kind of iteration to find the substring
unless:
1. The source string has fixed length fields.
or 2. The string is lexicographical ly order so that one
can use other search algorithms.


If the pattern, which the OP showed us, is fixed with respect
of the xxxx... part, it should be easy to just advance the start
pointer manually like pstart = &origin[9] or so.

But to find the length of the string, some sort of iterative or
recursive looping will certainly be unavoidable.

Once the length is known, and the pattern at the end,
again pstop could be found as something like
pstop=origin[lengthOfSTring-7].

I agree to you, that this always implies a sort of looping.

Depending on how the string was gathered,
there might be another chance if the OP could keep track of
the length (maybe a counter variable).
But that's implementation issue, not having to do with the
features of C.

Bernhard
Nov 14 '05 #6
"Magix" <ma***@asia.com > wrote in message news:<40******* ***@news.tm.net .my>...
hi,

let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


Why is a for loop not an option? If this is for work, then a solution
that works correctly and is reasonably efficient should be fine. If
this is for work, and your requirements are micro-managed to this
level so that you can't use for loops, I'd look for a new job.
Otherwise, this is homework.
Nov 14 '05 #7
On Tue, 06 Jul 2004 15:27:16 +0200, Bernhard Holzmayer
<ho************ ****@deadspam.c om> wrote:
Thomas Matthews wrote:
So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.


I guessed that the OP didn't want to code explicit for loops,
because of poor performance, but that (hopefully optimized)
functions like strchr() would be acceptable.


My guess was that it's a homework question.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #8
Magix wrote:
hi,

let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


How about:

char buff[] = "+CCED: \"xxxxxxxxx\ ", 333";
char* result = 1+strchr(buff, '"');
*strrchr(result , '"')=0;
Nov 14 '05 #9
I was trying to optimize the code performance, to see there is other option
better than "for" loop.

"red floyd" <re********@yah oo.com> wrote in message
news:96******** *************** *@posting.googl e.com...
"Magix" <ma***@asia.com > wrote in message

news:<40******* ***@news.tm.net .my>...
hi,

let say I have this string

+CCED: "xxxxxxxxx" , 333

What is the most efficient way to capture the string within the " " ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


Why is a for loop not an option? If this is for work, then a solution
that works correctly and is reasonably efficient should be fine. If
this is for work, and your requirements are micro-managed to this
level so that you can't use for loops, I'd look for a new job.
Otherwise, this is homework.

Nov 14 '05 #10

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

Similar topics

1
1632
by: Generic Usenet Account | last post by:
I had a need to tokenize a string, and this is what I came up with. Is this the most efficient way of tokenizing, or is a more efficient solution possible? Thanks, Gary
9
2255
by: lkrubner | last post by:
I've got a function, you can see it below, that is being called onmouseup in the textarea on my main form. The idea is to find a selection if possible and store that text in a global variable. I can't get this to work in any browser on a Mac, though it works alright on a PC. What am I missing?
7
2207
by: junky_fellow | last post by:
Can anyone suggest some efficient way to search a substring in a text file. For eg. suppose i want to search the pattern "abc" in a text file, then which algorithm should i use. I just want some hints, not the complete program. thanx in advance.....
29
51756
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
7
5585
by: jaylucier | last post by:
Howdy, I'm trying to break an input string into multpile pieces using a series of delimiters that start with an asterisk. Following the asterisk is a mulitple character identifier immediately followed by a data string of variable length. The input string may contain more than one identifier anywhere in the string. Here is an example: *CZ1 2.3 4-56 *fuuuS24364 08 23 72
6
7239
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to display an image. document.images only seems to reference image tags. The collection needs to include background images, input type = image, image maps, css assigned background, etc. Honestly, I am probably not aware of all the possibilities...
53
3435
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there anything like that in c? The closest thing I can find is strchr, but it check for a character,
9
2532
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered into my program and I want to see if any of a set of strings is a substring of the string I have been given. It's quite OK to stop at the first one found. Ideally the strings being searched through will be the keys of a dictionary but this...
3
2591
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I have coded using Substrings below? string s = TextBox1.Text.ToUpper(); string ch = s.Substring(0, 1); // read first character if ((ch == "B") || (ch == "C") || (ch == "X")) {
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.