CSE 1004 - Module 6 - Strings
CSE 1004 - Module 6 - Strings
module 6: strings
Faculty: Mrs Daphna Chacko, Assistant Professor, SCOPE.
1
Introduction
➢ A string literal is a series of characters that is enclosed in double quotes. For eg,
“Java”
➢ String is a sequence of characters
➢ In Java, strings are objects of the String class.
➢ The String type is not a primitive type. It is known as a reference type.
➢ The variable declared by a reference type is known as a reference variable that
references an object.
➢ String str = "Java Programming";
○ Here str is an object of the class String. i.e, str is a reference variable that references a String object
with contents Java Programming.
➢ A string can even have zero characters. Such a string is called the empty string and is
written as a pair of adjacent double quotes ("").
➢ The string " " is not empty: It consists of one blank character
Daphna Chacko CSE1004 4
Some in built methods in String class
● Any characters that are not visible when displayed are collectively known as
whitespace. Such characters include blanks, tabs, and new-line characters.
● The characters ' ', \t, \f, \r, or \n are known as whitespace characters.
For example, "\t Hello World \n".trim() returns a new string Hello World.
○ name = "Andhra " + name; // The value of the string “Pradesh’ is not changed
● To read an entire line of text i.e, a string that ends with the Enter key pressed, invoke
the nextLine() method i.e. sc.nextLine()
● To read a character from the console, use the nextLine() method to read a string and
then invoke the charAt(0) method on the string to return a character i.e.,
sc.nextLine().charAt(0)
● If the string is not a numeric string, the conversion would cause a runtime error.
● regionMatches(boolean ignoreCase, int ind, String str, int strIndex, int len)
● Returns true if the substring str starting at strIndex and of length specified by len is same
as the substring of this String object starting at ind and having the same length. Here the
region is compared regardless of case if ignoreCase is true
○ String source = "hello world";
○ String anotherString = "World of java";
○ System.out.println(source.regionMatches(6, anotherString, 0, 5));
○ System.out.println(source.regionMatches(true,6, anotherString, 0, 5));