简体   繁体   中英

get substring from string in C

I am trying to write a program in C, which will allow me to get a string I want between two other strings that will be defined. To be more specific, my example string is

 "blahblah<mailto:agent007@example.org>blahblahblah"

and I need to be able to extract the "agent007" substring to a new variable. I have tried the strtok() method, but the thing is I can't extract the tokens to a new variable or an array. I have tokenized the string and the statement that would suite me fine would be something like " if token[i] == "mailto" && token[i+2] == "example" then mailAdd = token[i+1] " (in a pseudo-code way :) )

my program so far

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="blahblah<mailto:agent007@example.org>blahblahblah";
  char * tch;
  tch = strtok (str,"<:@");
  while (tch != NULL)
  { 
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:@");
  }
  return 0;
}

Of course, any other suggestion beyond tokens will be greatly appreciated -

My first thought was to use strstr for "mailto:" and strchr for the '@'

// pseudo code
char *mailto = strstr(src, "mailto:"); // possibly convert src to lowercase
char *atsign = strchr(mailto, '@');
while (mailto < atsign) *dst++ = *mailto++;

Of course that is a very rough draft. It needs lots of refining (failure to find the "mailto:" string or '@' char, error-checking, special cases, tests, ...)


Saving the strtok pointer

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="blahblah<mailto:agent007@example.org>blahblahblah";
  char * tch;
  char * saved;                     /* pmg */
  tch = strtok (str,"<:@");
  while (tch != NULL)
  { 
    int savenext = 0;               /* pmg */
    if (!strcmp(tch, "mailto"))     /* pmg, UNTESTED CODE, need to check case? */
    {                               /* pmg */
      savenext = 1;                 /* pmg */
    }                               /* pmg */
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:@");
    if (savenext == 1)              /* pmg, UNTESTED CODE */
    {                               /* pmg */
      saved = tch;                  /* pmg */
    }                               /* pmg */
  }
  printf ("saved: %s\n", saved);    /* pmg */
  return 0;
}

You could use strstr to search for 'mailto:' and then strchr to search for '@' and take the characters in between. I never use strtok but I don't see what's wrong with what you've done.

Here's an example where email should point to "agent007" in your case. Error handling is missing here. This is destructive, meaning that it modifies the input string, but so does strtok .

char *mailto = strstr( str, "mailto:" );
char *at = strchr( mailto, '@' );
char *email = mailto + strlen("mailto:");
*at = '\0';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM