Presentation is loading. Please wait.

Presentation is loading. Please wait.

Additional string operators

Similar presentations


Presentation on theme: "Additional string operators"— Presentation transcript:

1 Additional string operators
length size find substr

2 The “length” function If we apply length function to a string, it will return the number of characters in that string. Examples: string firstName; string lastname; firstName = “Cynthia”; cout << firstName . length() << endl; // prints 7 lastName = “Lokker”; Cout << lastName . Length() << endl; // prints 6

3 Error message If you forget to use dot notation and write simply:
length() You will get a syntax error message: “undeclared identifier.” Compiler will think you are trying to call an ordinary function, named length without declaration. Compiler will not recognize that you are trying to call the length function associated with the string type.

4 The find function We use “find” function to find the first occurrence of a particular Substring. The function “find” returns an integer to indicate the position of the substring in the searching string. Examples: string str; str = “Introduction to programming”; Function calls Value returned by function str . find(“tion”) str . find(“Intro”) str . find(“prog”) Str . Find(“or”) string::npos, a largest possible value

5 The substr function We use the “substr” function to get a particular substring of a string. The “substr” function has two parameters: first parameter gives the position of the first character of the substring in the string second parameter gives the length of the substring. Examples: string str; str = “Introduction to programming”; Function call Value returned by the function str . substr(0, 12) Introduction str . substr(8, 7) tion to str . substr(40, 10) None. Program terminates with an execution error


Download ppt "Additional string operators"

Similar presentations


Ads by Google