0% found this document useful (0 votes)
1 views28 pages

Strsearch, Modify, Buffer

The document provides an overview of Java programming concepts related to strings, including methods for searching (indexOf, lastIndexOf, contains) and modifying strings (substring, concat, replace, trim). It also introduces the StringBuffer and StringBuilder classes for creating mutable strings, highlighting their constructors and important methods. Additionally, a quiz link is provided for further assessment.

Uploaded by

rajkumarm
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)
1 views28 pages

Strsearch, Modify, Buffer

The document provides an overview of Java programming concepts related to strings, including methods for searching (indexOf, lastIndexOf, contains) and modifying strings (substring, concat, replace, trim). It also introduces the StringBuffer and StringBuilder classes for creating mutable strings, highlighting their constructors and important methods. Additionally, a quiz link is provided for further assessment.

Uploaded by

rajkumarm
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/ 28

Course Title

JAVA PROGRAMMING

Dr.N.Baskar
1. String
Searching
2. String
Modification
3. String
Buffer

Dr.N.Baskar
String Searching
indexOf() , lastIndexOf() , contains()
indexOf() - find the first occurrence of a particular
character or substring in a string
Syntax:
Example:
There are 4 indexOf() methods: public class idxmain
public int indexOf(String str) { public static void main(String[] args)
public int indexOf(String str, int fromIndex) {String myStr = "Hello planet earth, you are a
public int indexOf(int char)
great planet.";
public int indexOf(int char, int fromIndex)
System.out.println(myStr.indexOf("e", 5));
Str - A String value, representing the string to }
search for
fromIndex - An int value, representing the index }
position to start the search for
Char - An int value, representing a single
character, e.g ‘A’, or a Unicode value
Dr.N.Baskar
String Searching
Example:
lastIndexOf() - find the last occurrence of a
particular character or substring in a string public class lastindex
Syntax {
There are 4 lastIndexOf() methods: public static void main(String[] args) {
public int lastIndexOf(String str) String myStr = "Hello planet earth, you are
a great planet.";
public int lastIndexOf(String str, int
fromIndex)
System.out.println(myStr.lastIndexOf("e"));
public int lastIndexOf(int char)
}
public int lastIndexOf(int char, int fromIndex)
}

Dr.N.Baskar
String Searching
contains() method searches the sequence of
characters in the given string.
It returns true if sequence of char values are found in class containexample
this string otherwise returns false. { public static void main(String args[])
Syntax : { String s1 = "My name is BAS";
public boolean contains(CharSequence sequence) System.out.println(s1.contains(“BAS"));
Parameter : System.out.println(s1.contains(“Bas"));
sequence : This is the sequence of characters to be }
searched.
Exception : }

NullPointerException : If seq is null

Implementation:
public boolean contains(CharSequence sequence)
{
return indexOf(sequence.toString()) > -1;
}
Dr.N.Baskar
Modifying a String
Substring() , concat() , replace() , trim()
 Strings are immutable i.e, once created they
cannot be changed.
 Thus, to modify use the following methods;
1) substring(): Extract a part of originally
declared string / string object. This method can
be used in two ways:-

a) public String substring(int startIndex)


b) public String substring
(int startIndex, int endIndex)
Parameters:
startIndex : starting index is inclusive
endIndex : ending index is exclusive

Dr.N.Baskar
Modifying a String

 2) concat() method combines specified string at


the end of this string. It returns combined
string. It is like appending another string.
Syntax
public String concat(String anotherString)

Parameter:
anotherString : another string i.e. to be
combined at the end of this string.

Dr.N.Baskar
Modifying a String
 3) replace(): This method is used to modify the
original string by replacing some characters
from it.
 Replace the characters in two ways:
a) String replace(char original, char
replacement): This method replaces one and
only character from the original string.
Syntax
public String replace(char oldChar, char newChar)

Parameters
oldChar : old character
newChar : new character

Dr.N.Baskar
Modifying a String
 b) String replace(CharSequence original,
CharSequence replacement): Unlike the
above method, which replaces only one
character at a time, using this method you can
replace a sequence of characters in one go.

Syntax
public String replace(CharSequence target, CharS
equence replacement)
Parameters
target : target sequence of characters
replacement : replacement sequence of
characters

Dr.N.Baskar
Modifying a String

4) trim(): The java string


trim() method eliminates leading
and trailing spaces.

Syntax
public String trim()

Dr.N.Baskar
concat() method combines specified string at the _________of this
string

Answer: C) end

Dr.N.Baskar
String Buffer
Java StringBuffer class is used to create  StringBuffer and StringBuilder classes are used for
creating mutable string.
mutable (modifiable) string.
Important Constructors of StringBuffer class
 The StringBuffer class in java is same as
String class except it is mutable i.e. it can be Constructor Description

changed. StringBuffer() creates an empty string buffer with the initial

It
capacity of 16.
contains some particular sequence of
characters, but the length and content of the StringBuffer(String str) creates a string buffer with the specified string.

sequence can be changed through certain


method calls. StringBuffer(int capacity) creates an empty string buffer with the specified
capacity as length.

They are safe for use by multiple threads.


Every string buffer has a capacity.

Dr.N.Baskar
String
Important methods of StringBuffer class
Buffer

Dr.N.Baskar
String Buffer Methods
1) StringBuffer append() method 2) StringBuffer insert() method
The append() method concatenates the The insert() method inserts the given string
given argument with this string. with this string at the given position.
class StringBufferExample{ class StringBufferExample2{
public static void main(String args[]){ public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello "); StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java"); sb.insert(1,"Java");
//now original string is changed //now original string is changed
System.out.println(sb); //prints Hello Java System.out.println(sb); //prints HJavaello
} }
} }

Dr.N.Baskar
String Buffer Methods
3) StringBuffer replace() method 4) StringBuffer delete() method
The replace() method replaces the given The delete() method of StringBuffer class
string from the specified beginIndex and deletes the string from the specified
endIndex. beginIndex to endIndex.
class StringBufferExample3{ class StringBufferExample4{
public static void main(String args[]){ public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello"); StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java"); sb.delete(1,3);
System.out.println(sb); //prints HJavalo System.out.println(sb); //prints Hlo
} }
} }

Dr.N.Baskar
String Buffer Methods
6) StringBuffer capacity() method
5) StringBuffer reverse() method
The capacity() method of StringBuffer class returns the current
The reverse() method of StringBuffer class capacity of the buffer.
reverses the current string. The default capacity of the buffer is 16. If the number of character
increases from its current capacity, it increases the capacity by
(oldcapacity*2)+2.
class StringBufferExample5{
For example if your current capacity is 16, it will be
public static void main(String args[]){ (16*2)+2=34.
class StringBufferExample6
StringBuffer sb=new StringBuffer("Hello");
{public static void main(String args[]){
sb.reverse();
StringBuffer sb=new StringBuffer();
System.out.println(sb); //prints olleH System.out.println(sb.capacity()); //default 16
sb.append("Hello");
}
System.out.println(sb.capacity()); //now 16
} sb.append("java is my favourite language");
System.out.println(sb.capacity());
//now (16*2)+2=34 i.e (oldcapacity*2)+2
} }
Dr.N.Baskar
String Buffer Methods

7) StringBuffer ensureCapacity() method

The ensureCapacity() method of StringBuffer class ensures that the given capacity
is the minimum to the current capacity.
If it is greater than the current capacity, it increases the capacity by
(oldcapacity*2)+2.
For example if current capacity is 16, it will be (16*2)+2=34.

Dr.N.Baskar
String Buffer Methods

Dr.N.Baskar
The default capacity of the buffer is ________.

Answer: B) 16

Dr.N.Baskar
StringBuilder class

Java StringBuilder class is used to create mutable (modifiable) string.


The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized.
It is available since JDK 1.5.
Important Constructors of StringBuilder class
Constructor Description

StringBuilder() creates an empty string Builder with the


initial capacity of 16.
StringBuilder(String str) creates a string Builder with the specified
string.
StringBuilder(int length) creates an empty string Builder with the
specified capacity as length.

Dr.N.Baskar
StringBuilder Methods

Dr.N.Baskar
StringBuilder Methods

1) StringBuilder append() method • 2) StringBuilder insert() method


The StringBuilder append() method concatenates • The StringBuilder insert() method inserts the given
the given argument with this string. string with this string at the given position.
• class StringBuilderExample2{
class StringBuilderExample
{ • public static void main(String args[]){
public static void main(String args[]){ • StringBuilder sb=new StringBuilder("Hello ");
StringBuilder sb=new StringBuilder("Hello "); • sb.insert(1,"Java");//now original string is changed
sb.append("Java");
//now original string is changed • System.out.println(sb);//prints HJavaello
System.out.println(sb);//prints Hello Java • }
}
}
• }

Dr.N.Baskar
StringBuilder Methods

3) StringBuilder replace() method 4) StringBuilder delete() method


The StringBuilder replace() method replaces the The delete() method of StringBuilder class deletes the string
given string from the specified beginIndex and from the specified beginIndex to endIndex.
endIndex.
class StringBuilderExample4{
class StringBuilderExample3{
public static void main(String args[]){
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
StringBuilder sb=new StringBuilder("Hello");
sb.delete(1,3);
sb.replace(1,3,"Java");
System.out.println(sb);//prints Hlo
System.out.println(sb);//prints HJavalo
}
}
}
}

Dr.N.Baskar
StringBuilder Methods

5) StringBuilder reverse() method 6) StringBuilder capacity() method


The reverse() method of StringBuilder class The capacity() method of StringBuilder class returns the current
reverses the current string. capacity of the Builder.
class StringBuilderExample5{ The default capacity of the Builder is 16.
public static void main(String args[]){ If the number of character increases from its current capacity, it
StringBuilder sb=new StringBuilder("Hello"); increases the capacity by (oldcapacity*2)+2.

sb.reverse(); For example if current capacity is 16, it will be (16*2)+2=34.


class StringBuilderExample6{
System.out.println(sb);//prints olleH
public static void main(String args[]){
} StringBuilder sb=new StringBuilder();
} System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());
Dr.N.Baskar
//now (16*2)+2=34 i.e (oldcapacity*2)+2
}}
StringBuilder Methods
7) StringBuilder ensureCapacity() method
The ensureCapacity() method of StringBuilder
class ensures that the given capacity is the
minimum to the current capacity. sb.append("java is my favourite language");

If it is greater than the current capacity, it System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldc


increases the capacity by (oldcapacity*2)+2. apacity*2)+2

For example if current capacity is 16, it will be sb.ensureCapacity(10);//now no change


(16*2)+2=34. System.out.println(sb.capacity());//now 34
class StringBuilderExample7{ sb.ensureCapacity(50);//now (34*2)+2
public static void main(String args[]){ System.out.println(sb.capacity());//now 70
StringBuilder sb=new StringBuilder(); }
System.out.println(sb.capacity());//default 16 }
sb.append("Hello");
System.out.println(sb.capacity());//now 16
Dr.N.Baskar
Summary
String Searching Methods: indexOf(), lastIndexOf(), contains()
String Modifying Methods : Substring() , concat() , replace() , trim()
StringBuffer and StringBuilder classes are used for creating mutable string.
Java StringBuffer class is used to create mutable (modifiable) string.
o Important Constructors of StringBuffer class
o Important methods of StringBuffer class
The Java StringBuilder class is same as StringBuffer class except that it is non-
synchronized. It is available since JDK 1.5.
o Important Constructors of StringBuilder class
o Important methods of StringBuilder class

Dr.N.Baskar
Quiz

Scan QR Code (or) Link

https://forms.gle/7RPZ13Dc99v6GPqf8

Dr.N.Baskar
Dr.N.Baskar

You might also like