Extracting a substring

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply vginer (51) 1/20/2010 2:50:44 PM

See related articles to this posting

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
Reply 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
Reply 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
Reply Andrew 1/20/2010 4:35:29 PM
comp.lang.c++ 48147 articles. 26 followers. Post

3 Replies
245 Views

Similar Articles

[PageSpeed] 7


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

extract substring
Hi, I tried to use awk extract username and domain name from an email address. but my first ever awk script doesn't work the way I want. Here is my example: #Usaged: sh extract.sh <ENTER> #!/bin/sh #cut -d "@" -f1 #awk -F"@" '{print $1; print $2}' user="" domain="" awk -F"@" '{user=$1}' awk -F"@" '{domain=$2}' #username=sed -e 's|@*||' print $user, $domain Where in the script go wrong? Thanks S On Wed, 17 Jan 2007 22:02:15 +1100, Pet <pp@modulus.org> wrote: >Hi, > >I t...

extract substring
Hi, I would like to extract 'some string' from the following string: <A href=\"#\">some string</A> Attention! This is not an HTML element, it is a STRING, so I can't do ..innerHTML. 'some string' can be of any length. Can you help ? thanks samuelberthelot@googlemail.com wrote: > I would like to extract 'some string' from the following string: > > <A href=\"#\">some string</A> <snip> var string = ('<A href=\"#\">some string</A>').substirng(12, 23); > Can you help ? H...

Extract with substring
Hi, I have the following input file (about 15K lines): 10.2.3.4 id1 Johnson 10.3.5.6 id2 Lewis 10.7.12.23 id3 Parker 10.2.3.4 id4 Johnsson 10.2.3.4 id5 Johnseen 10.7.12.1 id6 Johnsin 10.3.15.7 id7 Lewis-Junior ..... ------------------------ The output: ------------------------ John+ 10.2.3.4 id1 -> Johnson id4 -> Johnsson id5 -> Johnseen 10.7.12.1 id6 -> Johnsin Lewis+ 10.3.5.6 id2 -> Lewis 10.3.15.7 id7 -> Lewis-Junior ...... ...

substring extraction
Dear all, I have files of type: "xxxx.yyyy.zzzz", where ".zzzz" corresponds to the file's type and everything before corresponds to the file's name. By using awk, I wanted to extract the file's name without the file's extension => xxxx.yyyy. My initial solution looks like: echo xxxx.yyyy.zzzz | awk '{ myind=index($0,"\."); myvar=substr($0,0,myind-1); print myind " -> " myvar; }' Since "index" retrieves the very first occurrence of the ".", I would obtain: xxxx Is there any efficient way to retrieve the...

substr extracting
I want to extract 'ny' part from the string. 'n' being any number. #!/usr/bin/perl use strict; my $string = "$obj->time('<=','1y')"; if ($string =~ /time/) { extract($string); } sub extract { my $string = shift; $diff = substr ($string, -4,2); print $diff; } Two problems: 1. I get this error when using strict: Global symbol "$obj" requires explicit package name at a.pl line 3. The "$obj" is part of the string. So I will need it. Not sure how to avoid this error. 2. Sometime the string can cont...

SUbstring Extraction
I have a program that reads input from the user in the form Name("ABC") Addr("DEF") I need to check if Name was mentioned and extract ABC and store it in a variable and if Addr was mentioned and extract DEF and store that in another variable. Eg: if the user inputs Name("Joe") I need to check if he has mentioned Name and extract Joe The user can input Name("Joe") Addr("unit 2 sheperton") in this case i need to extract Joe and unit 2 sheperton and perform the appropirtae functions depending on whether Name or Addr or Both are mentioned. Tha...

how to extract substring
Hi, I have the following string (read from a file). } lrsstuv; I need to extract the lrsstuv part. The bracket will always be there. The number of spaces after the bracket varies. And at last the ';' is always after there after the name. Any hints on how to do this? I am using jdk 1.4 cheers, //mikael > I have the following string (read from a file). > > } lrsstuv; > > I need to extract the lrsstuv part. The bracket will always be there. > The number of spaces after the bracket varies. And at last the ';' is > always after there after the nam...

extracting a substring
Hi, I have a bunch of strings like a53bc_531.txt a53bc_2285.txt .... a53bc_359.txt and I want to extract the numbers 531, 2285, ...,359. One thing for sure is that these numbers are the ONLY part that is changing; all the other characters are always fixed. I know I should use regular expressions, but I'm not familar with python, so any quick help would help, such as which commands or idioms to use. Thanks a lot! b83503104@yahoo.com wrote: >Hi, >I have a bunch of strings like >a53bc_531.txt >a53bc_2285.txt >... >a53bc_359.txt > >and I want to extract the numbe...

Extracting substring with regexp
How to extract substring with regexp when we have start and end forsubstring?For example I want to find what is between "abc" and "xyz" in theString. Pattern p = Pattern.compile("abc*xyz"); Matcher m = p.matcher("aaaaaaaaaabc123xyzzzzzzzzzzzzz"); but m.matches() returns false and can't find my patter. How can I get these "123" in this example?Alex Kizub. On Jan 31, 3:52=A0pm, Alex <aki...@yahoo.com> wrote:> How to extract substring with regexp when we have start and end for> substring?> For example I want to find what is be...