Download presentation
Presentation is loading. Please wait.
Published bySusanto Kurniawan Modified over 6 years ago
1
F4105 JAVA PROGRAMMING F4105 Java Programming Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0
2
3.0 CLASS, POLYMORPHISM AND INHERITANCE
3.4 String
3
LEARNING OUTCOMES TOPIC 3.4
F4105 JAVA PROGRAMMING By the end of this chapter students shall be able to : Declare and manipulate data of char type. 1 Explain String references as parameters 2 Differentiate between the String and StringBuffer class. 3 Construct programme using method in class String 4
4
Introduction A string is a sequence of characters.
F4105 JAVAPROGRAMMING Introduction A string is a sequence of characters.
5
String Declaration Example 1 char str [ ] = {'a','e','i','o','u'};
F4105 JAVAPROGRAMMING String Declaration Example 1 char str [ ] = {'a','e','i','o','u'};
6
String Declaration Example 2
F4105 JAVAPROGRAMMING 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
class String class StringBuffer
F4105 JAVAPROGRAMMING java.lang Package class String class StringBuffer method method
8
String Class String class is present in java.lang package.
F4105 JAVAPROGRAMMING 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. Example: String obj = new String("Welcome"); obj.append("_World"); System.out.println(obj); wrong
9
Creating String Object
F4105 JAVAPROGRAMMING Creating String Object Example 1: Syntax String stringobject = new String( ); Example: String s=new String(“Java”);
10
Creating String Object
F4105 JAVAPROGRAMMING Creating String Object Example 2 Syntax String stringobject = <string value>; Example: String s = "I have to study for my exams";
11
Creating String Object
F4105 JAVAPROGRAMMING Creating String Object Example 3 Syntax String stringobject = new String(array_name); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr);
12
Creating String Object
F4105 JAVAPROGRAMMING Creating String Object Example 4 Syntax String stringobject = new String(stringobject); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr); String s1 = new String(s);
13
F4105 JAVAPROGRAMMING String Methods The String class has several methods that can be used to manipulate the string values. Each method has its own characteristic.
14
length() method Is used to find the number of characters in a string.
F4105 JAVAPROGRAMMING length() method Is used to find the number of characters in a string.
15
length() method Example 1 String s = "I am going to school";
F4105 JAVAPROGRAMMING length() method Example 1 String s = "I am going to school"; int numchar = s.length( ); System.out.println(numchar); Output 20
16
length() method Example 2 String s = " "; int numchar = s.length( );
F4105 JAVAPROGRAMMING length() method Example 2 String s = " "; int numchar = s.length( ); System.out.println(numchar); Output 2
17
length() method Syntax int variable_name = stringobject.length( );
F4105 JAVAPROGRAMMING length() method Syntax int variable_name = stringobject.length( );
18
concat() method Example 1 String firststring = "The Twin Tower";
F4105 JAVAPROGRAMMING concat() method Example 1 String firststring = "The Twin Tower"; String secondstring = " looks beautiful"; String thirdstring = firststring.concat(secondstring); @ firststring+secondstring Output The Twin Tower looks beautiful
19
concat() method Syntax
F4105 JAVAPROGRAMMING concat() method Syntax String Stringvariable3 = stringvariable1.concat (stringvariable2);
20
concat() method Example 2 String s1 = "Result: "+5+1;
F4105 JAVAPROGRAMMING concat() method Example 2 String s1 = "Result: "+5+1; System.out.println(s1); Output Result: 51
21
concat() method Example 3 String s2 = "Result: "+(5+1);
F4105 JAVAPROGRAMMING concat() method Example 3 String s2 = "Result: "+(5+1); System.out.println(s2); Output Result: 6
22
charAt() method Extracts a single character from a string
F4105 JAVAPROGRAMMING charAt() method Extracts a single character from a string
23
charAt() method Example String s = "Malaysia" char ch = s.charAt(4);
F4105 JAVAPROGRAMMING charAt() method Example String s = "Malaysia" char ch = s.charAt(4); System.out.println(ch); Output y
24
charAt() method Syntax
F4105 JAVAPROGRAMMING charAt() method Syntax char variablename = stringobject.charAt(int where);
25
equals() method Compares two strings for equality.
F4105 JAVAPROGRAMMING equals() method Compares two strings for equality.
26
equals() method Example String s1="Both are equal";
F4105 JAVAPROGRAMMING 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
27
equals() method Syntax
F4105 JAVAPROGRAMMING equals() method Syntax boolean variablename = stringobject.equals(stringobject);
28
F4105 JAVAPROGRAMMING indexOf() method Will search the first occurrence of a character or set of characters inside a string.
29
indexOf() method Example 1 int val1 = s.indexOf('s');
F4105 JAVAPROGRAMMING indexOf() method Example 1 int val1 = s.indexOf('s'); System.out.println(val1); Output 3
30
indexOf() method Example 2 int val2 = s.indexOf("is");
F4105 JAVAPROGRAMMING indexOf() method Example 2 int val2 = s.indexOf("is"); System.out.println(val2); Output 2
31
indexOf() method Syntax
F4105 JAVAPROGRAMMING indexOf() method Syntax int variablename = stringobject.indexOf(character); int variablename = stringobject.indexOf(string);
32
F4105 JAVAPROGRAMMING lastIndexOf() method Will search the last occurrence of a character or a set of characters inside a string.
33
lastIndexOf() method Example 1 int val1 = s.lastIndexOf('s');
F4105 JAVAPROGRAMMING lastIndexOf() method Example 1 int val1 = s.lastIndexOf('s'); System.out.println(val1); Output 12
34
lastIndexOf() method Example 2 int val2 = s.lastIndexOf("is");
F4105 JAVAPROGRAMMING lastIndexOf() method Example 2 int val2 = s.lastIndexOf("is"); System.out.println(val1); Output 5
35
lastIndexOf() method Syntax
F4105 JAVAPROGRAMMING lastIndexOf() method Syntax int variablename = stringobject.lastIndexOf(character); int variablename = stringobject.lastIndexOf(string);
36
F4105 JAVAPROGRAMMING replace() method Will replace all occurrences of a character in a string with another character.
37
replace() method Example String s="Hewwo";
F4105 JAVAPROGRAMMING 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 Hewwo Hello
38
replace() method Syntax
F4105 JAVAPROGRAMMING replace() method Syntax String stringvariable = stringobject.replace(original,replacement);
39
F4105 JAVAPROGRAMMING trim() method Will trim the leading and trailing white spaces in a string.
40
trim() method Example String s=" Hello Malaysia ";
F4105 JAVAPROGRAMMING trim() method Example String s=" Hello Malaysia "; String s1 = s.trim( ); System.out.println(s1); Output Hello Malaysia
41
trim() method Syntax String stringvariable = stringobject.trim( );
F4105 JAVAPROGRAMMING trim() method Syntax String stringvariable = stringobject.trim( );
42
F4105 JAVAPROGRAMMING toLowerCase() method Converts all the characters in a string from uppercase to lowercase.
43
toLowerCase() method Example String s1="LOWER";
F4105 JAVAPROGRAMMING toLowerCase() method Example String s1="LOWER"; String s2 = s1.toLowerCase( ); System.out.println(s2); Output lower
44
toLowerCase() method Syntax
F4105 JAVAPROGRAMMING toLowerCase() method Syntax String stringvariable = stringobject.toLowerCase( );
45
F4105 JAVAPROGRAMMING toUpperCase() method Converts all the characters in a string from lowercase to uppercase.
46
toUpperCase() method Example String s1="upper";
F4105 JAVAPROGRAMMING toUpperCase() method Example String s1="upper"; String s2 = s1.toUpperCase( ); System.out.println(s2); Output UPPER
47
toUpperCase() method Syntax
F4105 JAVAPROGRAMMING toUpperCase() method Syntax String stringvariable = stringobject.toUpperCase( );
48
substring() method Extracts a part of the string.
F4105 JAVAPROGRAMMING substring() method Extracts a part of the string.
49
substring() method Example String s1="I do shopping in Mega Mall";
F4105 JAVAPROGRAMMING substring() method Example String s1="I do shopping in Mega Mall"; String s2 = s1.substring(5,12); System.out.println(s2); Akrasa ke (5) sehingga aksara ke(12-1) Output shoppin
50
StringBuffer Class Is a special class supported by Java to handle strings. StringBuffer creates strings of flexible length whereas String class creates strings of fixed length.
51
Creating StringBuffer Objects
Example 1 StringBuffer sb1 = new StringBuffer(); Syntax StringBuffer StringBufferobject = new StringBuffer();
52
Creating StringBuffer Objects
Example 2 StringBuffer s = new StringBuffer(6); Syntax StringBuffer StringBufferobject = new StringBuffer(integervariable);
53
Creating StringBuffer Objects
Example 3 StringBuffer sb3 = new StringBuffer ("Java"); Syntax StringBuffer StringBufferobject = new StringBuffer(stringvariable);
54
length() method Finds the current length of a StringBuffer object.
55
length() method Example StringBuffer sb1 = new StringBuffer(“Java”);
System.out.println (sb1.length()); Output 4
56
capacity() method Returns the amount of memory allocated for the StringBuffer object.
57
capacity() method Example Output 30
StringBuffer sb = new StringBuffer("Capacity of sb"); System.out.println (sb.capacity()); * “Capacity of sb” = (free storage) = 30 (amount of storage) Output 30
58
capacity() method Example Output 16
StringBuffer sb1 = new StringBuffer(""); System.out.println (sb1.capacity()); * “Capacity of sb” = 16(free storage) = 16 (amount of storage) Output 16
59
append() method Allows data to be added to the end of a StringBuffer object.
60
append() method Example
StringBuffer sb1 = new StringBuffer("Welcome to beautiful"); sb1.append("_World"); System.out.println(sb1); Output Welcome to beautiful_World
61
setCharAt() method Sets the character at any position within a StringBuffer.
62
setCharAt() method Example
StringBuffer sb1 = new StringBuffer("Hello World"); sb1.setCharAt(5,'_'); System.out.println(sb1); Output Hello_World
63
insert() method Allows data to be added at a specified position in the StringBuffer object.
64
insert() method Example
StringBuffer sb1 = new StringBuffer("Hello World"); sb1.insert(6,"Java "); System.out.println(sb1); Output Hello Java World
65
reverse() method Example StringBuffer sb=new StringBuffer("roweT");
sb.reverse( ); System.out.println(sb); Output Tower
66
delete() method Example StringBuffer sb=new StringBuffer(“Program");
sb.delete(2, 5 ); //(2,(5-1)) System.out.print(sb); Output Pram
67
deleteCharAt() method
Example StringBuffer sb=new StringBuffer(“Java"); sb.deleteCharAt(1 ); System.out.print(sb); Output Jva
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.