Strsearch, Modify, Buffer
Strsearch, Modify, Buffer
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 : }
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:-
Dr.N.Baskar
Modifying a String
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
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
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.
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
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
Dr.N.Baskar
StringBuilder Methods
Dr.N.Baskar
StringBuilder Methods
Dr.N.Baskar
StringBuilder Methods
Dr.N.Baskar
StringBuilder Methods
Dr.N.Baskar
Quiz
https://forms.gle/7RPZ13Dc99v6GPqf8
Dr.N.Baskar
Dr.N.Baskar