Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ String Tutorial
07-12-2006, 05:07 AM (This post was last modified: 08-01-2006 02:03 AM by cmckni3.)
Post: #1
C++ String Tutorial
This tutorial will explain the various functions in C and C++ for string operations. I will give some examples but due to the overwelming amount of functions and their variations I can't give you an example for each but most of them are the same. I will start with C string functions which can also be used in C++.

Code:
#include <string>
//compare string a to string b and see if they are an exact match
if(strcmp(a,b)==0)
{
cout<<"Match";
//strings are an exact match
}
else
{
cout<<"No Match";
//strings are not an exact match
}

Code:
#include <string>
string c = "Hello";
//c = 5
c = strlen(a);

Code:
#include <string>
char str = "now # is the time for all # good men to come to the # aid of their country";
   char delims = "#";
   char *result = NULL;
   result = strtok( str, delims );
   while( result != NULL ) {
       printf( "result is \"%s\"\n", result );
       result = strtok( NULL, delims );
   }

Code:
#include <string>
strcpy(a, b);
//copies string a to string b

Code:
#include <string>
strcat (a, b);
//adds contents of string b to string a also called concatenating

Code:
void    *memccpy(void *, const void *, int, size_t);
void    *memchr(const void *, int, size_t);
int      memcmp(const void *, const void *, size_t);
void    *memcpy(void *, const void *, size_t);
void    *memmove(void *, const void *, size_t);
void    *memset(void *, int, size_t);
char    *strcat(char *, const char *);
char    *strchr(const char *, int);
int      strcmp(const char *, const char *);
int      strcoll(const char *, const char *);
char    *strcpy(char *, const char *);
size_t   strcspn(const char *, const char *);
char    *strdup(const char *);
char    *strerror(int);
size_t   strlen(const char *);
char    *strncat(char *, const char *, size_t);
int      strncmp(const char *, const char *, size_t);
char    *strncpy(char *, const char *, size_t);
char    *strpbrk(const char *, const char *);
char    *strrchr(const char *, int);
size_t   strspn(const char *, const char *);
char    *strstr(const char *, const char *);
char    *strtok(char *, const char *);
char    *strtok_r(char *, const char *, char **);
size_t   strxfrm(char *, const char *, size_t);

memchr Search buffer for a character
memcmp Compare two buffers
memcpy Copy bytes to buffer from buffer
memmove Copy bytes to buffer from buffer
memset Fill buffer with specified character
strcat Append string
strchr Find character in string
strcmp Compare two strings
strcoll Compare two strings using locale settings
strcpy Copy string
strcspn Search string for occurrence of charcter set
strerror Get pointer to error message string
strlen Return string length
strncat Append substring to string
strncmp Compare some characters of two strings
strncpy Copy characters from one string to another
strpbrk Scan string for specified characters
strrchr Find last occurrence of character in string
strspn Get length of substring composed of given characters
strstr Find substring
strtok Sequentially truncate string if delimiter is found
strxfrm Transform string using locale settings

Now for the C++ string functions. Note: C++ string functions can't be used with C but C functions may be used with C++.

Code:
string();
  string( const string& s );//empty string
  string( size_type length, const char& ch );//see str1
  string( const char* str );//see str2
  string( const char* str, size_type length );//same as str2 except allows specifying the number of characters
  string( const string& str, size_type index, size_type length );//see str3
  string( input_iterator start, input_iterator end );//a string of characters denoted by start and end
  ~string();

examples
string str1( 5, '1' );//Produces 5 copies of 1 in a row 11111
   string str2( "Hello World" );//str2 a constant string of Hello
   string str3( str2, 6, 5 );//creates a substring by going forward 6 characters on str2 and then stores the next 5 in str3 producing World

string& append( const string& str );
  string& append( const char* str );
  string& append( const string& str, size_type index, size_type len );//goes to index specified and appends the len of characters to string
  string& append( const char* str, size_type num );//appends a string to a string a specified number of times
  string& append( size_type num, char ch );//same as appending a string to string except this time a char is appended to a string a specified number of times see append example
  string& append( input_iterator start, input_iterator end );//same as constructor except it appends the sequence

append example
string hello = "The story continues";
name.append(3, '.');

Now I will list only the important C++ string functions and what they do.

Code:
String constructors create strings from arrays of characters and other strings
String operators concatenate strings, assign strings, use strings for I/O, compare strings
append append characters and strings onto a string
assign give a string values from strings of characters and other C++ strings
capacity returns the number of elements that the string can hold
clear removes all elements from the string
compare compares two strings
copy copies characters from a string into an array
empty true if the string has no elements  
erase removes elements from a string
find find characters in the string
find_first_not_of find first absence of characters
find_first_of find first occurrence of characters
find_last_not_of find last absence of characters
find_last_of find last occurrence of characters
getline read data from an I/O stream into a string
insert insert characters into a string
length returns the length of the string
max_size returns the maximum number of elements that the string can hold
push_back add an element to the end of the string  
replace replace characters in the string
reserve sets the minimum capacity of the string
resize change the size of the string
rfind find the last occurrence of a substring
size returns the number of items in the string
substr returns a certain substring
swap swap the contents of a string with another

If you like this tutorial please register.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: