Access individual character

Web Site  Methods

 
Str::GetFirstChar
 
STRCHAR GetFirstChar () const

Returns the first character in the string.  If the string is empty, will return 0 (the null character).

 

 
Str::GetLastChar
 
STRCHAR GetLastChar () const

Returns the last character in the string. If the string is empty, will throw an exception (error condition).

 

 
Str::GetAt
Equivalent to: operator []
 
STRCHAR GetAt (CPOS idx) const
STRCHAR operator[] (const CPOS idx) const

The GetAt() method, and its equivalent indexing operator, will retrieve a single character from the string at the given 0-based position. If the position is less than 0, or beyond the end of the string, the null character (0) is returned.

Example: compare a C string with a Str string character-by-character

   LPCTSTR s_src = _T("SaMpLe 123");
Str s (s_src);
for (int i=0; i<s.GetLength(); i++) {
STRCHAR c1 = s_src[i];
STRCHAR c2 = s[i]; // Equivalent to s.GetAt(i)
ASSERT(c1 == c2);
}

 

See also: Basic operations