LECTURE – STRING FUNCTIONS
OOP (JAVA)
MR. PREM KUMAR
LECTURER
DEPARTMENT OF CYBER SECURITY
Dawood University of Engineering and Technology, Karachi
AGENDA
• String
• String Built in Functions
• Programming Examples
PREM KUMAR 4/8/2026 2
Strings
Strings are used for storing text.
A string variable contains a collection of characters surrounded by double quotes:
Example: Result:
PREM KUMAR 4/8/2026
3
Strings
There are many string methods available, for example toUpperCase() and
toLowerCase():
Example: Result:
PREM KUMAR 4/8/2026
4
Strings
Finding a Character in a String
The indexOf() method returns the index (the position) of the first occurrence of a
specified text in a string (including whitespace):
Example: Result:
PREM KUMAR 4/8/2026
5
String Length
A String is an object, which contain methods that can perform certain operations on
strings.
The length of a string can be found with the length() method:
Example:
PREM KUMAR 4/8/2026
Result: 6
String Concatenation
The + operator can be used between strings to combine them.
Example:
Result:
PREM KUMAR 4/8/2026
7
String Concatenation
The concat() method to concatenate two strings:
Example:
Result:
PREM KUMAR 4/8/2026
8
String Special Characters
Because strings must be written within quotes, Java will misunderstand this string,
and generate an error:
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\) escape character turns special characters into string characters:
PREM KUMAR 4/8/2026
9
String Special Characters
The sequence \” inserts a double quote in a string:
Example:
Result:
PREM KUMAR 4/8/2026
10
String Special Characters
The sequence \’ inserts a single quote in a string:
Example:
Result:
PREM KUMAR 4/8/2026
11
String Special Characters
The sequence \\ inserts a single backslash in a string:
Example:
Result:
PREM KUMAR 4/8/2026
12
String Special Characters
The sequence \\ inserts a single backslash in a string:
Code Result
\n New Line
\r Carriage Return
\t Tab
\b Backspace
PREM KUMAR 4/8/2026
Strings
Adding Numbers and Strings
Add two numbers, the result will be a number:
Example: Result:
PREM KUMAR 4/8/2026
Strings
Adding Numbers and Strings
Add two strings, the result will be a string concatenation:
Example: Result:
PREM KUMAR 4/8/2026
Strings
Adding Numbers and Strings
Add a number and a string, the result will be a string concatenation:
Example: Result:
PREM KUMAR 4/8/2026
Thanks ☺
PREM KUMAR 4/8/2026 17