-
Extracting a sub-String
I am new to C and I have a requirement that is causing a few problems. I have to extract a substring from a string.
The substring is a date in the form of YYYYMMDDHHMMSS and I know that the string I need to extract this from is in the form of...
GR_YYYYMMDDHHMMSS_nnnn
Where the GR_ bit is always constant in any string that my code will process.
Since I know the start position and length (4 and 14 respectively) of the bit I require I thought it would be easy. I was kind of expecting a string function which allowed me to nominate these values and return to me the value I require.
Reading through various sources there doesn't seem to any such function. Can someone give me some advice
:confused:
Thanks in Advance
Mike
-
How about strncpy ?
Code:
const char s1[] = "GR_YYYYMMDDHHMMSS_nnnn";
char s2[15];
strncpy(s2, s1+3, 14);
s2[14] = '\0';