0% found this document useful (0 votes)
16 views25 pages

Week 6 - L16-L18

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views25 pages

Week 6 - L16-L18

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Subject Name: OOP

Unit No:3
Unit Name: Arrays and String Class

Faculty Name : Mr. Dayanand Dhongade


Index

Lecture 16 – String Class-I 3

Lecture 17 – String Class-II 10

Lecture 18 – String Buffer Class 16

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

4 Lecture 16 – String Class-I


String constructors

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

String s4=new String(char A[], int startIndex, int numChars)


Eg:- char A[]={‘n’,’e’,’r’,’u’,’l’};
String city=new String(A,0,3);

String s5=new String(String s4)

5 Lecture 16 – String Class-I


Methods of String class

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

6 Lecture 16 – String Class-I


Methods of String class

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.

7 Lecture 16 – String Class-I


Methods of String class

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

8 Lecture 16 – String Class-I


Methods of String class

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

9 Lecture 16 – String Class-I


Module No 3: Arrays and String Class

Lecture No: 17
String Class-II
Demo of String Methods

class StringDemo{ //TO FIND LENGTH OF A STRING


public static void main(String args[]) System.out.println("Length of String
{ s1:"+s1.length());
String s0="JAVA PROGRAM ";
String s1="Java Program "; //TO FIND CHARACTER AT SPECIFIC
String s3=" Welcome To "; INDEX
System.out.println("String s0:"+s0); System.out.println("Character at Index 2
in String s1:"+s1.charAt(2));
System.out.println("String s1:"+s1);
System.out.println("String s3:"+s3);
//TO FIND INDEX OF FIRST
CHARACTER OCCURENCE
System.out.println("First occurrence of
Character 'a' in String
s1:"+s1.indexOf("a"));

11 Lecture 17 – String Class-II


Demo of String Methods

//TO FIND INDEX OF LAST //TO CONVERT STRING TO


CHARACTER OCCURENCE CHARACTER ARRAY
System.out.println("Last occurrence of A=s1.toCharArray();
Character 'a' in String System.out.print("String Conversion into
s1:"+s1.lastIndexOf("a")); Array: ");
for(int i=0;i<A.length;i++)
//TO FIND SUBSTRING FROM {
SPECIFIC INDEX System.out.print(A[i]);
int start=5,end=8; }
char A[]=new char[end-start]; System.out.println();
s1.getChars(start,end,A,0);
String s2=new String(A);
System.out.println("String s2:"+s2);

12 Lecture 17 – String Class-II


Demo of String Methods

//TO FIND OUT SUBSTRING //TO COMPARE TWO STRINGS WITH


System.out.println("Sub-String of String IGNORE CASE
s1 from Index 5:"+s1.substring(5)); if(s1.equalsIgnoreCase(s0))
System.out.println("Sub-String of String { System.out.println("Compare String s1
s1 from Index 9 to Index and s0 with Ignore Case: "+s1+" ==
12:"+s1.substring(9,12)); "+s0);
}
//TO COMPARE TWO STRINGS FOR else
EQUALITY { System.out.println("Compare String s1
if(s1.equals(s2)) and s0 with Ignore Case:"+s1+" !=
{System.out.println("Compare String s1 "+s0);
and s2 for Equality: "+s1+" == "+s2); }
}
else
{System.out.println("Compare String s1
and s2 for Equality: "+s1+" != "+s2);
}
13 Lecture 17 – String Class-II
Demo of String Methods

//TO COMPARE TWO STRINGS //TO REPLACE ONE CHARACTER BY


int a=s2.compareTo(s1); OTHER
System.out.println("Comparison Result System.out.println("Replace character A
of String s2 and s1:"+a); with a in String
s0:"+s0.replace('A','a'));
//TO CONCAT TWO STRINGS
System.out.println("Concatenate String //CONVERT TO LOWER CASE
s3 and s1:"+s3.concat(s1)); System.out.println("Convert String s1
into Lower case:
"+s1.toLowerCase());
//TO REMOVE LEADING AND
TRAILING WHITE SPACES
System.out.println("Remove Leading //CONVERT TO UPPER CASE
and Trailing Spaces of String System.out.println("Convert String s1
s3:"+s3.trim()); into upper case:
"+s1.toUpperCase());
}
}
14 Lecture 17 – String Class-II
Demo of String Methods

OUTPUT

String s0:JAVA PROGRAM


String s1:Java Program
String s3: Welcome To
Length of String s1:16
Character at Index 2 in String s1:v
First occurrence of Character 'a' in String s1:1
Last occurrence of Character 'a' in String s1:10
String s2:Pro
String Conversion into Array: Java Program
Sub-String of String s1 from Index 5:Program
Sub-String of String s1 from Index 9 to Index 12:ram
Compare String s1 and s2 for Equality: Java Program != Pro
Compare String s1 and s0 with Ignore Case: Java Program == JAVA PROGRAM
Comparison Result of String s2 and s1:6
Concatenate String s3 and s1: Welcome To Java Program
Remove Leading and Trailing Spaces of String s3:Welcome To
Replace character A with a in String s0:JaVa PROGRaM
Convert String s1 into Lower case: java program
Convert String s1 into upper case: JAVA PROGRAM

15 Lecture 17 – String Class-II


Module No 3: Arrays and String Class

Lecture No: 18
String Buffer class
StringBuffer

• represents growable and writeable character sequences


• may have characters and substrings inserted in the middle or appended
to the end
• has more characters pre-allocated than are actually needed, to allow
room for growth

• https://www.youtube.com/watch?v=_bX7jslx6rY

17 Lecture 18 – String Buffer Class


StringBuffer Constructors

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

Note- allocates room for 16 additional characters when no specific


buffer length is requested

18 Lecture 18 – String Buffer Class


StringBuffer class methods

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

19 Lecture 18 – String Buffer Class


StringBuffer class methods

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

20 Lecture 18 – String Buffer Class


Demo of StringBuffer Methods

class StringBufferDemo //TO DISPLAY CAPACITY


{ public static void main(String System.out.println("Capacity of String
args[]) s1: "+s1.capacity());
{ StringBuffer s1=new System.out.println("Capacity of String
StringBuffer(15); s2: "+s2.capacity());
String s=new String("java");
StringBuffer s2=new StringBuffer(s); //TO SET BUFFER CAPACITY
System.out.println("String s1: "+s1); s1.ensureCapacity(20);
System.out.println("String s2: "+s2); System.out.println("Set Buffer
Capacity of String s1:
//TO DISPLAY LENGTH "+s1.capacity());
System.out.println("Length of String
s1: "+s1.length());
System.out.println("Length of String
s2: "+s2.length());

21 Lecture 18 – String Buffer Class


Demo of StringBuffer Methods

//TO SET/REPLACE CHARACTER AT //TO INSERT STRING


GIVEN POSITION s1.insert(0,"hello");
s2.setCharAt(1,'A'); System.out.println("Insert 'hello' at the
System.out.println("Replace character beginning of String s1: "+s1);
at position 1 with A in String s2:
"+s2); //TO REVERSE A STRING
s1.reverse();
//TO APPEND STRING System.out.println("Reverse the String
s2.append(" program"); s1: "+s1);
System.out.println("Append 'program'
at the end of String s2: "+s2);

22 Lecture 18 – String Buffer Class


Demo of StringBuffer Methods

//TO DELETE A SUBSTRING //TO REPLACE SUBSTRING


s2.delete(10,12); s2.replace(0,4,"java hi ");
System.out.println("Delete a substring System.out.println("Replace a
from position 10 to position 12 in substring from position 0 to position
String s2: "+s2); 4 in String s2 with 'java hi': "+s2);
}
//TO DELETE CHARACTER AT }
SPECIFIC INDEX'
s2.deleteCharAt(1);
System.out.println("Delete a character
at position 1 in String s2: "+s2);

23 Lecture 18 – String Buffer Class


Demo of StringBuffer Methods

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

24 Lecture 18 – String Buffer Class


Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy