DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More String Errors - String(C++)

Element Selection and Substrings

The next group of operators allows extraction of arbitrary elements of Strings and substrings. Substring and character selection and assignment also provide the means to do insertion, deletion, and replacement on characters of a String. One String can be inserted into another by assigning to a zero-length substring, a substring can be deleted from a String by assigning the empty String to it, and any substring can be replaced by an arbitrary String or substring.

The arguments to these functions must always stay within the bounds of the String, otherwise their behavior is undefined.

A special type (Substring) is used in the implementation primarily for communication within the compiler. You should never declare variables of this type, and you can usually ignore its existence, unless you are using implicit s from String to a user-defined type.


[]

char& String::operator[](unsigned);

The semantics of the subscript operator are the same as ordinary array subscripting. s[n] is the n+1th character of String s. Following the usual C conventions, the first element of s is s[0]. Strings are stored contiguously so the [] operator is satisfactory for character-by-character processing. This operator, because it returns a reference to a character, can be used either to extract a character from a String, for example, char c = s[i];, or to replace a character of a String, for example, s[i] = c;.


char_at

char String::char_at(unsigned) const;

s.char_at(i) is the ith character of String s. This function is more efficient than operator[]() when just extracting a character. Also, this function can be used on a const String.


()

Substring& String::operator()(unsigned,unsigned);

s(offset,length) is the substring of String s starting offset characters from the beginning and length characters long. The substring returned will be automatically converted to the equivalent String, unless the substring expression is the left operand of an assignment. In that case, the right operand replaces the indicated substring of String s. s(offset) is the substring of s starting offset characters from the beginning and running through the end of s.


=

String Substring::operator=(const String&);

This version of the assignment operator is called when the left operand is a substring. It implements substring replacement for Strings (when s(m,n) is used as the left operand of =).


chunk

String String::chunk(unsigned,unsigned) const;

s.chunk(offset,length) returns a String consisting of a copy of the characters of String s starting offset characters from the beginning and length characters long. This function is more efficient than operator()() when just extracting characters from a String. Also, this function can be used on a const String. s.chunk(offset) returns the String consisting of a copy of the characters of s starting offset characters from the beginning and running through the end of s.

The following are some examples of element extraction and replacement:

   String ix = "01234";
   ix[2] = '9';               // now ix is "01934"
   String x = ix.char_at(3);  // x is "3"
   ix[0] = ix[4];             // now ix is "41934"
   cout << ix[5];             // out of bounds!
                              // behavior is undefined

The following are examples of substring selection and replacement:

   String s1 = "abcd", s2;
   s2 = s1.chunk(1,2);       // now s2 is "bc"
   s1(1,1) = s2;             // now s1 is "abccd"
   s1(2,0) = s1(1,4);        // now s1 is "abbccdccd"
   s1(2,5) = ";             // now s1 is "abcd"
   s1(0,0) = "111";          // now s1 is "111abcd"

Next topic: Comparison Operations
Previous topic: Input and Output

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005