f



Extracting a substring

Hello. This is a basic question, but I haven't been able to find out
the answer by my own.

I have a variable which is a char array, I mean:


    char line_p[80] ;


How can I get the substring from the 1st character to the 10th (for
example) and store it in another char array ??

I've found the method "substring" or something similar, but that's for
"string" objects, which is not my case.

Thank you in advance!

--
Vicent
0
vginer (51)
1/20/2010 2:50:44 PM
comp.lang.c++ 49148 articles. 1 followers. Post Follow

3 Replies
391 Views

Similar Articles

[PageSpeed] 26

On Jan 20, 6:50=A0am, Vicent Giner-Bosch <vgi...@gmail.com> wrote:
> Hello. This is a basic question, but I haven't been able to find out
> the answer by my own.
>
> I have a variable which is a char array, I mean:
>
> =A0 =A0 char line_p[80] ;
>
> How can I get the substring from the 1st character to the 10th (for
> example) and store it in another char array ??
>
> I've found the method "substring" or something similar, but that's for
> "string" objects, which is not my case.
>
If you really must use a char array (C-style string) instead of a
string, look at these:
strncpy
memcpy
memmove

--
Fred K
0
Fred
1/20/2010 3:25:31 PM
> If you really must use a char array (C-style string) instead of a
> string, look at these:
> strncpy
> memcpy
> memmove

Well, I prefer using C-style strings.

I've understood the use of strncpy, thanks.

Two more questions:

(1) What if I want to get the substring from the 3rd to the 7th
position (for example)?

(2) Is there any "wrap" function for C-style strings, I mean, any
function for taking away the white spaces at the beginning at the end
of a string?

Thank you in advance.

--
Vicent
0
Vicent
1/20/2010 4:20:04 PM
On 2010-01-20, Vicent Giner-Bosch <vginer@gmail.com> wrote:
>
>> If you really must use a char array (C-style string) instead of a
>> string, look at these:
>> strncpy
>> memcpy
>> memmove
>
> Well, I prefer using C-style strings.
>

Then use C. C++ has much stricter typing than C, which
forces you to cast the return value of malloc (a risky
practice), and in general use more casts than is safe.

Also, you have to track string length yourself, which
breaks encapsulation and is also often slower.

0
Andrew
1/20/2010 4:35:29 PM
Reply: