字符串操作函数大全

cstring - Copying:

  • memcpy

    Copy block of memory (function )

    void * memcpy ( void * destination, const void * source, size_t num );
    
  • memmove

    Move block of memory (function )

    void * memmove ( void * destination, const void * source, size_t num );
    
  • strcpy

    Copy string (function )

    char * strcpy ( char * destination, const char * source );
    
  • strncpy

    Copy characters from string (function )

    char * strncpy ( char * destination, const char * source, size_t num );
    

cstring - Concatenation:

  • strcat

    Concatenate strings (function )

    char * strcat ( char * destination, const char * source );
    
  • strncat

    Append characters from string (function )

    char * strncat ( char * destination, const char * source, size_t num );
    

cstring - Comparison:

  • memcmp

    Compare two blocks of memory (function )

    int memcmp ( const void * ptr1, const void * ptr2, size_t num );
    
  • strcmp

    Compare two strings (function )

    int strcmp ( const char * str1, const char * str2 );
    
  • strcoll

    Compare two strings using locale (function )

    int strcoll ( const char * str1, const char * str2 );
    
  • strncmp

    Compare characters of two strings (function )

    int strncmp ( const char * str1, const char * str2, size_t num );
    
  • strxfrm

    Transform string using locale (function )

    size_t strxfrm ( char * destination, const char * source, size_t num );
    

cstring - Searching:

  • memchr

    Locate character in block of memory (function )

    const void * memchr ( const void * ptr, int value, size_t num );
          void * memchr (       void * ptr, int value, size_t num );
    
  • strchr

    Locate first occurrence of character in string (function )

    const char * strchr ( const char * str, int character );
          char * strchr (       char * str, int character );
    
  • strcspn

    Get span until character in string (function )

    size_t strcspn ( const char * str1, const char * str2 );
    
  • strpbrk

    Locate characters in string (function )

    const char * strpbrk ( const char * str1, const char * str2 );
          char * strpbrk (       char * str1, const char * str2 );
    
  • strrchr

    Locate last occurrence of character in string (function )

    const char * strrchr ( const char * str, int character );
          char * strrchr (       char * str, int character );
    
  • strspn

    Get span of character set in string (function )

    size_t strspn ( const char * str1, const char * str2 );
    
  • strstr

    Locate substring (function )

    const char * strstr ( const char * str1, const char * str2 );
          char * strstr (       char * str1, const char * str2 );
    
  • strtok

    Split string into tokens (function )

    char * strtok ( char * str, const char * delimiters );
    

    Example

    #include <stdio.h>
    #include <string.h>
      
    int main ()
    {
      char str[] ="- This, a sample string.";
      char * pch;
      pch = strtok (str," ,.-");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
      }
      return 0;
    }
    

cstring - Other:

  • memset

    Fill block of memory (function )

    void * memset ( void * ptr, int value, size_t num );
    
  • strerror

    Get pointer to error message string (function )

    char * strerror ( int errnum );
    

    Example

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
      
    int main ()
    {
      FILE * pFile;
      pFile = fopen ("unexist.ent","r");
      if (pFile == NULL)
        printf ("Error opening file unexist.ent: %s\n",strerror(errno));
      return 0;
    }
    
  • strlen

    Get string length (function )

    size_t strlen ( const char * str );
    
  • strdup

    Duplicate a string (linux)

    char *strdup(const char *s);
    char *strndup(const char *s, size_t n);
    char *strdupa(const char *s);
    char *strndupa(const char *s, size_t n);
    

cstdlib - String conversion:

  • atof

    Convert string to double (function )

    double atof (const char* str);
    
  • atoi

    Convert string to integer (function )

    int atoi (const char * str);
    
  • atol

    Convert string to long integer (function )

    long int atol ( const char * str );
    
  • atoll

    Convert string to long long integer (function )

  • strtod

    Convert string to double (function )

    double strtod (const char* str, char** endptr);
    
  • strtof

    Convert string to float (function )

  • strtol

    Convert string to long integer (function )

    long int strtol (const char* str, char** endptr, int base);
    

    Example

    #include <stdio.h>      /* printf */
    #include <stdlib.h>     /* strtol */
      
    int main ()
    {
      char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
      char * pEnd;
      long int li1, li2, li3, li4;
      li1 = strtol (szNumbers,&pEnd,10);
      li2 = strtol (pEnd,&pEnd,16);
      li3 = strtol (pEnd,&pEnd,2);
      li4 = strtol (pEnd,NULL,0);
      printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
      return 0;
    }
    
  • strtold

    Convert string to long double (function )

  • strtoll

    Convert string to long long integer (function )

  • strtoul

    Convert string to unsigned long integer (function )

    unsigned long int strtoul (const char* str, char** endptr, int base);
    
  • strtoull

    Convert string to unsigned long long integer (function )

cstdio - Formatted input/output:

  • snprintf

    Write formatted output to sized buffer (function )

  • sprintf

    Write formatted data to string (function )

    int sprintf ( char * str, const char * format, ... );
    
  • sscanf

    Read formatted data from string (function )

    int sscanf ( const char * s, const char * format, ...);
    

    Example

    #include <stdio.h>
      
    int main ()
    {
      char sentence []="Rudolph is 12 years old";
      char str [20];
      int i;
      
      sscanf (sentence,"%s %*s %d",str,&i);
      printf ("%s -> %d\n",str,i);
        
      return 0;
    }
    
  • vsnprintf

    Write formatted data from variable argument list to sized buffer (function )

  • vsprintf

    Write formatted data from variable argument list to string (function )

    int vsprintf (char * s, const char * format, va_list arg );
    
  • vsscanf

    Read formatted data from string into variable argument list (function )