Professional Documents
Culture Documents
Presentation 1
Revision
1. Mention a need for an array. 2. Give a difference between a onedimensional and two-dimensional array.
Objectives
At the end of this presentation, you will be able to: Declare and manipulate strings List various string methods and their usage
Introduction
A string is a sequence of characters.
String Declaration
Example 1
String Declaration
Example 2
String Class
String class is present in java.lang package.
The methods in this class are used to manipulate strings.
Hands-On!
Program stringdemo.java will declare string objects and display the strings.
String Methods
The String class has several methods that can be used to manipulate the string values.
Each method has its own characteristic.
length() method
Is used to find the number of characters in a string.
length() method
Example 1
20
length() method
Example 2
length() method
Syntax
Hands-On!
Program strlen.java will find the length of the given string.
concat() method
Example 1
String firststring = "The Twin Tower"; String secondstring = " looks beautiful"; String thirdstring = firststring.concat(secondstring);
Output
concat() method
Syntax
concat() method
Example 2
Result: 51
concat() method
Example 3
Result: 6
Hands-On!
Program strconcat.java will illustrate the use of concat() method.
charAt() method
Extracts a single character from a string
charAt() method
Example
charAt() method
Syntax
Hands-On!
Program extractchar.java will illustrate the use of charAt() method.
equals() method
Compares two strings for equality.
equals() method
Example
String s1="Both are equal"; String s2="Both are equal"; String s3="Both are not equal"; System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3));
Output
true false
equals() method
Syntax
Hands-On!
Program equalsdemo.java will compare two string and return the value based on the result.
indexOf() method
Will search the first occurrence of a character or set of characters inside a string.
indexOf() method
Example 1
indexOf() method
Example 2
indexOf() method
Syntax
lastIndexOf() method
Will search the last occurrence of a character or a set of characters inside a string.
lastIndexOf() method
Example 1
12
lastIndexOf() method
Example 2
lastIndexOf() method
Syntax
Hands-On!
Program occurstr.java will to illustrate the use indexOf() and lastIndexOf() method.
replace() method
Will replace all occurrences of a character in a string with another character.
replace() method
Example
String s="Hewwo"; String s1 = s.replace('w','l'); System.out.println(Old string= + s); System.out.println(New string= + s1);
Output
Hello
replace() method
Syntax
Hands-On!
Program repchar.java will illustrate the use of replace() method.
trim() method
Will trim the leading and trailing white spaces in a string.
trim() method
Example
Hello Malaysia
trim() method
Syntax
Hands-On!
Program trimspace.java will illustrate the
toLowerCase() method
Converts all the characters in a string from uppercase to lowercase.
toLowerCase() method
Example
lower
toLowerCase() method
Syntax
toUpperCase() method
Converts all the characters in a string from lowercase to uppercase.
toUpperCase() method
Example
UPPER
toUpperCase() method
Syntax
Hands-On!
Program convertstring.java will illustrate the use of toLowerCase() and toUpperCase() methods.
substring() method
Extracts a part of the string.
substring() method
Example
shopping
Hands-On!
Program substringdemo.java will illustrate the use of substring() method.
Activity 6.5.1
1. Consider the string declared using the statement given:
Activity 6.5.1
2. Examine the following code:
Activity 6.5.1
Which of the following statements can be used to store the string Wild Flower Rose in Result?
Lab Exercise
1. Write a program to declare a string of your own and find its length. 2. Write a program to replace a character by a given character in your string.
Summary
In this session, you learnt the following: A string is a sequence of characters. In Java, string is considered as a class. Objects of string class will be of string data type. Once you create string object you cannot change the characters that comprise the string.
Summary
In this session, you learnt the following: You can create a string instance using a string literal. For each string literal, Java automatically constructs a string object. The string class has several methods that can be used to manipulate the string values. There are methods to compare two strings, search for a substring, concatenate two strings and change the case of letters within a string.
Assignment
1. What is a string?
2. List any three methods in the String class.