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++ 48364 articles. 29 followers. Post

3 Replies
276 Views

Similar Articles

[PageSpeed] 0


  • 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...

Bash:Substring Extraction
Hi, Can you give me a way to get the substring from a variable that is portable to older versions of bash? When I try echo ${VAR:0:5} It works in a setup having 2.05b.0(1) release of bash but fails with another setup having 1.14.7(1) version. In the latter, I am getting the error: bash: ${VAR:0:5}: bad substitution I am not in a position to upgrade the bash version in the second machine. Thanks and regards, Karthick S. Karthick S. wrote: > Hi, > > Can you give me a way to get the substring from a variable that is > portable to older versions of bash?...

extracting substrings from a file
Hi, I have a file with several entries in the form: AFFX-BioB-5_at E. coli /GEN=bioB /gb:J04423.1 NOTE=SIF corresponding to nucleotides 2032-2305 of /gb:J04423.1 DEF=E.coli 7,8-diamino-pelargonic acid (bioA), biotin synthetase (bioB), 7-keto-8-amino-pelargonic acid synthetase (bioF), bioC protein, and dethiobiotin synthetase (bioD), complete cds. 1415785_a_at /gb:NM_009840.1 /DB_XREF=gi:6753327 /GEN=Cct8 /FEA=FLmRNA /CNT=482 /TID=Mm.17989.1 /TIER=FL+Stack /STK=281 /UG=Mm.17989 /LL=12469 /DEF=Mus musculus chaperonin subunit 8 (theta) (Cct8), mRNA. /PROD=chaperonin subunit 8 (theta) /FL...

Extracting a substring #2
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 p...

extract a specific substring
Hi to all, I'm an awk newbie. I have a text file like this: aaa /home/smith/subfolder1/filename1.ext1 bbb ccc /home/smith/subfolder1/subfolder2/filename2.ext2 ddd eee /home/smith/subfolder1/filename3.ext3 fff :::: I should extract (for further parsing) the filenames above: in other words the substrings between the LAST slash '/' (excluded) and the last space ' ' (excluded) filename1.ext1 filename2.ext2 filename3.ext3 ::::: thanks a lot Marco Marco Meoni wrote: > Hi to all, > I'm an awk newbie. > > I have a text file like this: > > aaa ...

regex: How to extract substrings?
This should be really easy, but I couldn't find it in the tutorials and documentations on the web: What is the Java equivalent to the following Perl operation? my $entry = "__3432__Smith__" my ($id,$name) = $entry =~ m/__(\d+)__([A-z]+)__/; In other words, I want to extract the number and the name from the string $entry, using a regular expression. I tried the following: Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__"); Matcher m = p.matcher("__3432__Smith__"); while(m.find()){ System.out.println(m.group()); } But that gives me the compl...

Blank substring extraction
IMPORTANT! click on "View Original Format" Suppose I have three strings; string1 = '23.12 45.67 100.01 3.1 3' string2 = '25.06 89.02 22.00 5.2 10' string3 = ' 48.10 23.50 ' How can I use input/formating functions to obtain: x(1) = [23.12; 25.06; 0] x(2) = [45.67; 0; 48.10] x(3) = [100.01; 89.02; 0] x(4) = [0; 22.0; 23.50] x(5) = [3.1; 5.2; 0] x(6) = [3; 10; 0] There are actually 6 substrings (corresponding to the 6-element vector x) in each of the strings. There is a "fixed field" width for each substring entry and one or more whitespaces ...

Extracting substring from string BASH
Hi all, I saw a lot of examples like those reported here http://tldp.org/LDP/abs/html/refcards.html or http://www.faqs.org/docs/abs/HTML/string-manipulation.html but I would like to extract a substring from the first time the character, or (the substring), occurs till the end of a mail string. I tried the following, hoping it worked but it didn't. A="Nel bel mezzo del cammin di nostra vita mi ritrovai in una selva oscura chje la diritta via era smarrita" expr match "$A" '.*\(mezzo*)' expr should return " del cammin di nostra vita mi ritrovai in una...

Extract substrings using metacharacters
Hello, I do not have much experience with Mathematica string functions, so this may be trivial: When I use a string pattern with metacharacters (like the patterns used for StringMatchQ[] or FileNames[]), is there a simple way to extract the substring that matches the metacharacters? (something similar to regular expressions and m// in perl?) I have a lot of similar but different file names that I read in with FileNames[pattern], and I would like to extract the different parts. At the moment I have the following workaround: files = {"data040523.dat","data040...

Stregex question
I am trying to extract a substring after a colon. My string looks like: xxxxxx:yyyyyyyyy where x's can be spaces, upper and lower case letters then there's the colon and finally the y's, which can be pretty much anything and will often inclu= de colons (which is why strsplit("xxxxxx:yyyyyyyyy", ":") won't work for me= here) I just want the yyyyyyyyyy part and have been trying to extract it with str= egex, but no luck. But before I give up and will try a different approach,= does anyone know how to construct a regular expression for IDL...

extract numbers and substring from a wxString?
Dear all, If I have a wxString,said A which is " Age 20 , Salary 1234.22 ", How can I get "Age", "20", "Salary", "1234.22"? Thanks for your help. Regards, cylin. cylin wrote: > Dear all, > > If I have a wxString,said A which is " Age 20 , Salary 1234.22 ", > How can I get "Age", "20", "Salary", "1234.22"? Try looking at boost.tokenizer. www.boost.org You won't even have to build the boost libraries to use the tokenizer function, as i...

Substring RegExp Extract path
Hi, I'm trying to extract element of a directory path stored in the db with substring "/help/support/index/time.jsp" and i want to extract the 1st, 2nd and 3rd parts 1st = help, 2nd = support, 3rd = index but I can't get the regular expressions correct. Any ideas on what I need to do. select substring(data,'????') from paths TIA Nick wrote: > "/help/support/index/time.jsp" > > and i want to extract the 1st, 2nd and 3rd parts > > 1st = help, 2nd = support, 3rd = index Are you using 7.4? If so: select split...

grep problem
I'm currently using this script: #!/bin/sh # # in - Finds and mounts USB camera and/or MP3Player/Flash Disk # camcontrol devlist | grep FLASH\ DISK > /dev/null 2>&1 if [ "$?" -eq "0" ]; then dev=`camcontrol devlist | grep FLASH\ DISK | cut -c 62-64` if [ `df | grep $dev | cut -c 6-8` ]; then echo MP3Player already mounted else mount_msdosfs /dev/${dev} /home/dave/mp3player ; echo Mounted MP3Player to ~/mp3player fi else echo MP3Player not plugged in fi camcontrol devlist | grep FUJIFILM > /dev/null 2>&1 if [ "$?" -eq...

Extract the substring between two different tokens.
Hi, Please let me know the code(script), to extract a substring between two different tokens. Ex: I/P: "UDP:[192.168.2.143]:6752" >From this input, i want extact only the IPaddress .i.e. O/P: 192.168.2.143. Thanks -Khan khan wrote: > Hi, > > Please let me know the code(script), to extract a substring between > two different tokens. > > Ex: I/P: "UDP:[192.168.2.143]:6752" >>From this input, i want extact only the IPaddress .i.e. > O/P: 192.168.2.143. > > Thanks > -Khan > If you can only have one of those patterns per line a...

extracting a substring from all elements in a cell array?
There may be a better way to store my data and I'm open to any and all suggestions (that help with this problem anyway :-) I have a cell array of strings. For each element in the array, I'd like to create a new cell array that contains characters 9:16 of the original array. For a single element, I can do xxx = cellarray{1}(9:16) but there doesn't seem to be a syntax that works for all elements. I know I can do it in a loop, but that just doesn't feel "right"... Any ideas? Thanks! Bruce Bruce Bowler <bbowler@bigelow.org> wrote in mess...

extracting specific substrings from a text-file
Hello! I have following text-file: ---INT--- 123 435 789 ---EST-- abc def ---SAT--- x234 y567 z01 Now I want to extract these substrings: a = '123, 456, 789' b = 'abc, def' c = 'x234, y567, z01' How can this problem be resolved? Greeting from Bavaria, Michael On Jun 19, 7:59 am, BavarianMike86 <jo.mich...@gmx.de> wrote: > Hello! > > I have following text-file: > > ---INT--- > 123 > 435 > 789 > > ---EST-- > abc > def > > ---SAT--- > x234 > y567 > z01 > > Now I want to extract these substrings: >...