[cs23021] substr() function

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Feb 20 17:51:13 EST 2012


CSI students,

here is a bit more information on substr(A,B)

The first argument A has to be greater than zero and less than the
length of the string; the second argument B, has to be less than A
minus the size of the string. The two parameters have default values
and can be skipped. The default value for A is zero and for B is
"whatever remains till the end of the string". For example:

   string s="Hello!";
   cout << s.substr()  << endl; // both arguments omitted, prints the whole string
   cout << s.substr(2) << endl; // second argument omitted, prints from
   	   	       	        // 3d character till end of string

If either of these parameters are out of range the behavior of this
function is not specified by the C++ standard. However, the program
can be designed to recover from these kinds of errors through
a mechanism called exceptions. We will study it later in the course.

Thanks,
-- 
Mikhail


More information about the cs23021-2 mailing list