JAVA Chapter4 Lecture Notes
JAVA Chapter4 Lecture Notes
COURSE TUTOR:
BASHIR ABDU MUZAKKARI, Ph.D.
This document does not claim any originality and cannot be used as
substitute for prescribed textbooks. The information presented here is
purely a collection by the course lecturer for teaching assignments.
Various textbooks as well as freely available materials from the internet
were consulted for preparing this document. The ownership of the
information lies with the respective authors or institutions.
Strings
The strings are a collection of characters bound by double quotes(“ ”).
They are immutable, i.e. once they are declared, their values cannot be
changed.
String can also be listed as an array of characters starting at the index 0 and
ending with a null character (\0). This means that the first letter of the string is
at index 0 to the last character of the string which is indexed at (length of the
string-1).
concat- As the name suggests, this method is useful for concatenating two
strings. Concatenating means adding together two entities.
It has the syntax of <String-variable1>.concat(<String_variable2>);
In place of the variables you can also use string literals directly.
You can also concat strings by simple add operator.
Example- “Hello”+ ” MyName”; OUTPUTS: Hello MyName.
Methods of Declaring Strings
indexOf- This method returns the index of the first occurrence of the
character passed as an argument in the string.
The syntax of the method is <string_variable1>.indexOf(<String_variable2>);
equals- This method returns true if both the strings are equal and false if they
are not equal.
It has a syntax of <String_variable1>.equals(<String_variable2>);
trim- Trims the string, i.e, removes all unnecessary spaces before and after
the string. Note that it does not remove the spaces inside the string.
It has a syntax like <variable>.trim();
replace- Returns the string by replacing all occurrences of the first character
with the second character
It has a syntax <String_variable>.replace(<character1>,<character2>);
Methods of Declaring Strings
1. package javaapplication1;
2. public static void main(String[] args) {
3. String s = " I am learning Java at YUMSUK! ";
4. System.out.println("The output of s.length() is " + s.length());
5. System.out.println("The output of s.charAt(10) is " + s.charAt(10));
6. System.out.println("The output of s.substring(4) is " + s.substring(4));
7. System.out.println("The output of s.substring(5,10) is " + s.substring(10, 18));
8. String s1 = "Data",
9. s2 = "Flair";
10. System.out.println("The output of s1.concat(s2) is " + s1.concat(s2));
11. System.out.println("The output of s.indexOf('D') is " + s.indexOf("D"));
12. System.out.println("The output of s.length() is " + s.length());
13. System.out.println("The output of s1.equals(s2) is " + s1.equals(s2));
14. System.out.println("The output of s1.compareTo(s2) is " + s1.compareTo(s2));
15. System.out.println("The output of s.toLowerCase() is " + s.toLowerCase());
16. System.out.println("The output of s.toUpperCase() is " + s.toUpperCase());
17. System.out.println("The output of s.trim() is " + s.trim());
18. }
Methods of Declaring Strings
The output of s.length() is 37
The output of s.charAt(10) is e
The output of s.substring(4) is I am learning Java at YUMSUK!
The output of s.substring(5, 10) is earning
The output of s1.concat(s2) is JavaYUMSUK
The output of s.indexOf('M') is 28
The output of s.length() is 37
The output of s1.equals(s2) is false
The output of s1.compareTo(s2) is -15
The output of s.toLowerCase() is i am learning java at yumsuk!
The output of s.toUpperCase() is I AM LEARNING JAVA AT YUMSUK!
The output of s.trim() is I am learning Java at YUMSUK!