topic 3 string part i
TRANSCRIPT
-
7/30/2019 Topic 3 String Part I
1/65
Programming Language (JAVA)
Unit 6.5 String Manipulation
Presentation 1
-
7/30/2019 Topic 3 String Part I
2/65
-
7/30/2019 Topic 3 String Part I
3/65
Objectives
At the end of this presentation, you will be able to:
Declare and manipulate strings List various string methods and their usage
-
7/30/2019 Topic 3 String Part I
4/65
Introduction
A string is a sequence of characters.
-
7/30/2019 Topic 3 String Part I
5/65
String Declaration
Example 1
char str [ ] = {'a','e','i','o','u'};
-
7/30/2019 Topic 3 String Part I
6/65
String Declaration
Example 2
char text[ ] = {'I','','C','A','N','','W','R','I','T',
'E','A','','G','O','O','D','','P','R','O','G','R','A','
M','I','N','','J','A','V','A'};
Example 3
String text = "I CAN WRITE A GOOD
PROGRAM IN JAVA";
-
7/30/2019 Topic 3 String Part I
7/65
String Class
String class is present in java.lang package.
The methods in this class are used to
manipulate strings.
The string objects created in a String class
cannot be changed.
-
7/30/2019 Topic 3 String Part I
8/65
Creating String Object
Example 1
String s = new String( );
Syntax
String stringobject = new String( );
-
7/30/2019 Topic 3 String Part I
9/65
Creating String Object
Example 2
char arr[ ] = {'H','E','L','L','O'};
String s = new String(arr);
Syntax
String stringobject = new String(array_name);
-
7/30/2019 Topic 3 String Part I
10/65
Creating String Object
Example 3
char arr[ ] = {'H','E','L','L','O'};
String s = new String(arr);
String s1 = new String(s);
Syntax
String stringobject = new
String(stringobject);
-
7/30/2019 Topic 3 String Part I
11/65
Creating String Object
Example 4
String s = "I have to study for my exams";
Syntax
String stringobject = ;
-
7/30/2019 Topic 3 String Part I
12/65
Hands-On!
Program stringdemo.java will declare string
objects and display the strings.
-
7/30/2019 Topic 3 String Part I
13/65
String Methods
The String class has several methods that
can be used to manipulate the string values.
Each method has its own characteristic.
-
7/30/2019 Topic 3 String Part I
14/65
length() method
Is used to find the number of characters in a
string.
-
7/30/2019 Topic 3 String Part I
15/65
length() method
Example 1
String s = "I am going to school";
int numchar = s.length( );
System.out.println(numchar);
Output
20
-
7/30/2019 Topic 3 String Part I
16/65
length() method
Example 2
String s = " ";
int numchar = s.length( );
System.out.println(numchar);
Output
2
-
7/30/2019 Topic 3 String Part I
17/65
length() method
Syntax
int variable_name = stringobject.length( );
-
7/30/2019 Topic 3 String Part I
18/65
Hands-On!
Program strlen.java will find the length of the
given string.
-
7/30/2019 Topic 3 String Part I
19/65
concat() method
Example 1
String firststring = "The Twin Tower";
String secondstring = " looks beautiful";
String thirdstring =
firststring.concat(secondstring);
Output
The Twin Tower looks beautiful
-
7/30/2019 Topic 3 String Part I
20/65
concat() method
Syntax
String Stringvariable3 = stringvariable1.concat
(stringvariable2);
-
7/30/2019 Topic 3 String Part I
21/65
concat() method
Example 2
String s1 = "Result: "+5+1;
System.out.println(s1);
Output
Result: 51
-
7/30/2019 Topic 3 String Part I
22/65
concat() method
Example3
String s2 = "Result: "+(5+1);
System.out.println(s2);Output
Result: 6
-
7/30/2019 Topic 3 String Part I
23/65
Hands-On!
Program strconcat.java will illustrate the use
ofconcat() method.
-
7/30/2019 Topic 3 String Part I
24/65
charAt() method
Extracts a single character from a string
-
7/30/2019 Topic 3 String Part I
25/65
charAt() method
Example
String s = "Malaysia"
char ch = s.charAt(4);
System.out.println(ch);
Output
y
-
7/30/2019 Topic 3 String Part I
26/65
charAt() method
Syntax
char variablename =
stringobject.charAt(int where);
-
7/30/2019 Topic 3 String Part I
27/65
Hands-On!
Program extractchar.java will illustrate the
use ofcharAt() method.
-
7/30/2019 Topic 3 String Part I
28/65
equals() method
Compares two strings for equality.
-
7/30/2019 Topic 3 String Part I
29/65
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
-
7/30/2019 Topic 3 String Part I
30/65
equals() method
Syntax
boolean variablename =
stringobject.equals(stringobject);
-
7/30/2019 Topic 3 String Part I
31/65
Hands-On!
Program equalsdemo.java will compare two
string and return the value based on the
result.
-
7/30/2019 Topic 3 String Part I
32/65
indexOf() method
Will search the first occurrence of a character
or set of characters inside a string.
-
7/30/2019 Topic 3 String Part I
33/65
Example 1
int val1 = s.indexOf('s');
System.out.println(val1);
Output3
indexOf() method
-
7/30/2019 Topic 3 String Part I
34/65
indexOf() method
Example 2
int val2 = s.indexOf("is");
System.out.println(val2);
Output2
-
7/30/2019 Topic 3 String Part I
35/65
indexOf() method
Syntax
int variablename =
stringobject.indexOf(character);
int variablename =
stringobject.indexOf(string);
-
7/30/2019 Topic 3 String Part I
36/65
lastIndexOf() method
Will search the last occurrence of a character
or a set of characters inside a string.
-
7/30/2019 Topic 3 String Part I
37/65
Example 1
int val1 = s.lastIndexOf('s');
System.out.println(val1);
Output12
lastIndexOf() method
-
7/30/2019 Topic 3 String Part I
38/65
lastIndexOf() method
Example 2
int val2 = s.lastIndexOf("is");
System.out.println(val1);
Output5
-
7/30/2019 Topic 3 String Part I
39/65
lastIndexOf() method
Syntax
int variablename =
stringobject.lastIndexOf(character);int variablename =
stringobject.lastIndexOf(string);
-
7/30/2019 Topic 3 String Part I
40/65
Hands-On!
Program occurstr.java will to illustrate the use
indexOf() and lastIndexOf() method.
-
7/30/2019 Topic 3 String Part I
41/65
replace() method
Will replace all occurrences of a character in
a string with another character.
-
7/30/2019 Topic 3 String Part I
42/65
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
-
7/30/2019 Topic 3 String Part I
43/65
replace() method
Syntax
String stringvariable =
stringobject.replace(original,replacement);
-
7/30/2019 Topic 3 String Part I
44/65
Hands-On!
Program repchar.java will illustrate the use of
replace() method.
-
7/30/2019 Topic 3 String Part I
45/65
trim() method
Will trim the leading and trailing white spaces
in a string.
-
7/30/2019 Topic 3 String Part I
46/65
trim() method
Example
String s=" Hello Malaysia ";
String s1 = s.trim( );
System.out.println(s1);
Output
Hello Malaysia
-
7/30/2019 Topic 3 String Part I
47/65
trim() method
Syntax
String stringvariable = stringobject.trim( );
-
7/30/2019 Topic 3 String Part I
48/65
Hands-On!
Program trimspace.java will illustrate the
use oftrim() method.
-
7/30/2019 Topic 3 String Part I
49/65
toLowerCase() method
Converts all the characters in a string from
uppercase to lowercase.
-
7/30/2019 Topic 3 String Part I
50/65
toLowerCase() method
Example
String s1="LOWER";
String s2 = s1.toLowerCase( );
System.out.println(s2);
Output
lower
-
7/30/2019 Topic 3 String Part I
51/65
toLowerCase() method
Syntax
String stringvariable =
stringobject.toLowerCase( );
-
7/30/2019 Topic 3 String Part I
52/65
toUpperCase() method
Converts all the characters in a string from
lowercase to uppercase.
-
7/30/2019 Topic 3 String Part I
53/65
toUpperCase() method
Example
String s1="upper";
String s2 = s1.toUpperCase( );
System.out.println(s2);
Output
UPPER
-
7/30/2019 Topic 3 String Part I
54/65
toUpperCase() method
Syntax
String stringvariable =
stringobject.toUpperCase( );
-
7/30/2019 Topic 3 String Part I
55/65
Hands-On!
Program convertstring.java will illustrate the
use oftoLowerCase() and toUpperCase()
methods.
-
7/30/2019 Topic 3 String Part I
56/65
substring() method
Extracts a part of the string.
-
7/30/2019 Topic 3 String Part I
57/65
substring() method
Example
String s1="I do shopping in Mega Mall";
String s2 = s1.substring(5,12);
System.out.println(s2);
Output
shopping
-
7/30/2019 Topic 3 String Part I
58/65
Hands-On!
Program substringdemo.java will illustrate the
use ofsubstring() method.
-
7/30/2019 Topic 3 String Part I
59/65
Activity 6.5.1
1. Consider the string declared using the
statement given:
String line = "The sky is blue in color";
Which of the following will convert the string
to lowercase:
a. String a = line.toLowerCase();
b. String b = toLowerCase(line);
-
7/30/2019 Topic 3 String Part I
60/65
Activity 6.5.1
2. Examine the following code:
String s1 = "Wild";
String s2 = "Flower";
String s3 = "Rose";
String Result;
-
7/30/2019 Topic 3 String Part I
61/65
Activity 6.5.1
Which of the following statements can be
used to store the string Wild Flower Rosein
Result?
Result = s1.concat( s2.concat( s3 ) );
Result.concat(s1,s2,s3);
-
7/30/2019 Topic 3 String Part I
62/65
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 agiven character in your string.
-
7/30/2019 Topic 3 String Part I
63/65
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 cannotchange the characters that comprise the
string.
-
7/30/2019 Topic 3 String Part I
64/65
Summary
In this session, you learnt the following:
You can create a string instance using a
string literal. For each string literal, Javaautomatically 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.
A i t
-
7/30/2019 Topic 3 String Part I
65/65
Assignment
1. What is a string?
2. List any three methods in the String class.