Week 6 - L16-L18
Week 6 - L16-L18
Unit No:3
Unit Name: Arrays and String Class
2
Module No 3: Arrays and String Class
Lecture No: 16
String Class-I
String
• Is a class in JAVA
• implements strings as objects of String class.
• Can not modify characters of the original string, hence its immutable.
• each time you need an altered version of an existing string, a new String
object is created that contains the modifications
Eg:-
String s1=“HELLO”;
https://www.youtube.com/watch?v=xcEe9GYEcA4
• Syntax:
String s2=new String(); //creates empty string
String s3=new String(char A[]);
Eg:- char A[]={‘n’,’e’,’r’,’u’,’l’};
String city=new String(A);
Function Explanation
int length() To find the number of characters from a string
char charAt(int ) To extract a single character from a String
int indexOf(int ch) To search for the first occurrence of a character,
int lastIndexOf(int ch) To search for the last occurrence of a character
void getChars(int sourceStart, int to extract more than one character at a time
sourceEnd, char target[ ], int
targetStart)
Char[] toCharArray() to convert all the characters in a String object
into a character array
Function Explanation
boolean equals(Object str) To compare two strings for equality, It returns
true if the strings contain the same characters
in the same order, and false otherwise.
boolean To perform a comparison that ignores case
equalsIgnoreCase(String str) differences, When it compares two strings, it
considers A-Z to be the same as a-z.
String substring(int startIndex) returns a copy of the substring that begins at
startIndex and runs to the end of the invoking
string
String substring(int startIndex, string returned contains all the characters from
the beginning index, up to, but not
int endIndex) including, the ending index.
Function Explanation
int For sorting applications, you need to know which is less
compareTo(String than, equal to, or greater than the next. A string is less
str) than another if it comes before the other in dictionary
order. A string is greater than another if it comes after the
other in dictionary order.
Less than zero :The invoking string is less than str.
Greater than zero :The invoking string is greater than str.
Zero : The two strings are equal.
String concat(String creates a new object that contains the invoking string with
str) the contents of str appended to the end
Function Explanation
String replace(char replaces all occurrences of one character in the invoking
original, string with another character
char replacement)
String trim( ) returns a copy of the invoking string from which any leading
and trailing whitespace has been removed.
String return a String object that contains the lowercase equivalent
toLowerCase() of the invoking String
String return a String object that contains the uppercase equivalent
toUpperCase() of the invoking String
Lecture No: 17
String Class-II
Demo of String Methods
OUTPUT
Lecture No: 18
String Buffer class
StringBuffer
• https://www.youtube.com/watch?v=_bX7jslx6rY
• StringBuffer( )
Eg:- StingBuffer s1=new StringBuffer();
• StringBuffer(int size)
Eg:- StingBuffer s1=new StringBuffer(15);
• StringBuffer(String str)
Eg:- String s=new String(“java”);
StingBuffer s1=new StringBuffer(s);
Functions Explanation
int length() Returns the current length of the StringBuffer
class
int capacity() Returns total allocated capacity of
StringBuffer
void ensureCapacity(int capacity) to set the size of the buffer
void setCharAt(int where, char ch) where specifies the index of the character
being set, and ch specifies the new value of
that character.
StringBuffer append(String str) concatenates the string representation of any
other type of data to the end of the invoking
StringBuffer object
Functions Explanation
StringBuffer insert(int index, String index specifies the index at which point the string
str) will be inserted into the invoking StringBuffer
object
StringBuffer reverse( ) reverse the characters within a StringBuffer object
StringBuffer delete(int startIndex, deletes a sequence of characters from the
int endIndex) invoking object
StringBuffer deleteCharAt(int loc) deletes the character at the index specified by loc
StringBuffer replace(int startIndex, The substring being replaced is specified by the
int endIndex, String str) indexes startIndex and endIndex. Thus, the
substring at startIndex through endIndex–1 is
replaced
OUTPUT
String s1:
String s2: java
Length of String s1: 0
Length of String s2: 4
Capacity of String s1: 15
Capacity of String s2: 20
Set Buffer Capacity of String s1: 32
Replace character at position 1 with A in String s2: jAva
Append 'program' at the end of String s2: jAva program
Insert 'hello' at the beginning of String s1: hello
Reverse the String s1: olleh
Delete a substring from position 10 to position 12 in String s2: jAva progr
Delete a character at position 1 in String s2: jva progr
Replace a substring from position 0 to position 4 in String s2 with 'java hi': java hi progr