0% found this document useful (0 votes)
35 views

JAVA Chapter4 Lecture Notes

This document provides an overview of string processing in Chapter 4 of the course CSC 2302: Computer Programming II. It discusses that strings are immutable collections of characters bound by double quotes and can be treated as character arrays. It then lists and explains several important string methods in Java for declaring, manipulating, and comparing strings, including length(), charAt(), substring(), concat(), indexOf(), equals(), compareTo(), toLowerCase(), toUpperCase(), and trim(). It provides examples of using each method.

Uploaded by

aabdurrahaman647
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

JAVA Chapter4 Lecture Notes

This document provides an overview of string processing in Chapter 4 of the course CSC 2302: Computer Programming II. It discusses that strings are immutable collections of characters bound by double quotes and can be treated as character arrays. It then lists and explains several important string methods in Java for declaring, manipulating, and comparing strings, including length(), charAt(), substring(), concat(), indexOf(), equals(), compareTo(), toLowerCase(), toUpperCase(), and trim(). It provides examples of using each method.

Uploaded by

aabdurrahaman647
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSC 2302: COMPUTER PROGRAMMING II

CHAPTER 4: STRING PROCESSING

COURSE TUTOR:
BASHIR ABDU MUZAKKARI, Ph.D.

DEPARTMENT OF COMPUTER SCIENCE


YUSUF MAITAMA SULE UNIVERSITY, KANO
DISCLAIMER

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).

This is an individual data type in Java.


String Methods in Java
Methods of Declaring Strings
 length- This method is particularly useful for finding out the length of the
string. It returns an integer which is the length of the string.
The basic syntax of this method is <string variable>.length();

 charAt- This returns the character at the particular index passed as an


argument to this method.
The syntax is <string variable>.charAt(<index>);

 substring(int i)- This method returns a string which is a substring of the


original string starting at the index passed as the argument to the method.
This has a simple syntax of <String variable>.substring(<index>);
Methods of Declaring Strings
 substring(int i, int j)- This method returns the substring which starts at the
index i, given as the first argument to the method and ends at j, given as the
second argument in the method.
It has a simple syntax of <String variable>.substring(<index1>, <index2>);

 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>);

 compareTo- This method compares the two strings in a lexicographical order.


If both of the strings are equal it returns zero. The result is positive if the first
argument string is greater than the second string, lexicographically. If not, the
result is negative.
It has a syntax of <variable1>.compareTo(<variable2>);
Methods of Declaring Strings
 toLowerCase- Converts the string to lowercase.
It has a syntax of <variable>.toLowerCase();

 toUpperCase- Converts the string to uppercase.


It has a syntax of <variable>.toUpperCase();

 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!

You might also like