Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
Stringbuilder
What you’ll Learn
String
String Methods
Stringbuilder
String
String str="Hello!";
Ways to Create String
0 1 2 3 4 5 6
E t h n u s \0
Method Description
char charAt(int index) returns char value for the particular index
boolean equals(Object another) checks the equality of string with the given object.
String replace(char old, char new) replaces all occurrences of the specified char value.
The java string charAt() method returns a char value at the given
index number
class Main {
public static void main(String args[]){
String name="codemithra";
char ch=name.charAt(4);
System.out.println(ch);
}
}
length() - Example
The java string length() method length of the string. It returns count
of total number of characters
class Main {
public static void main(String args[]){
String s1="ETHNUS";
String s2="Codemithra.com";
System.out.println("string length is:
"+s1.length());//6 is the length of ETHNUS string
System.out.println("string length is: "+s2.length());
//14 is the length of Codemithra.com string
}}
subString() - Example
class Main{
public static void main(String args[]){
String name="what do you know about me";
System.out.println(name.contains("do you know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}
}
endsWith - Example
The java string endsWith() method checks if this string ends with
given suffix
class Main{
public static void main(String args[]){
String s1="java by ethnus";
System.out.println(s1.endsWith("s"));
System.out.println(s1.endsWith("ethnus"));
}
}
Equals - Example
The java string indexOf() method returns index of given character value
or substring.
If it is not found, it returns -1. The index counter starts from zero.
There are 4 types of indexOf method in java
Method Description
int indexOf(int ch) returns index position for the given char value
int indexOf(int ch, int fromIndex) returns index position for the given char value and
from index
int indexOf(String substring) returns index position for the given substring
int indexOf(String substring, int returns index position for the given substring and
fromIndex) from index
indexOf() - Example
class Main {
public static void main(String[] args) {
String s1 = "This is indexOf method";
int index = s1.indexOf("method");
System.out.println("index of substring "+index);
}
}
isEmpty - Example
class Main{
public static void main(String args[]){
String s1="";
String s2="ethnus";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}
}
lastIndexOf() - Example
The java string lastIndexOf() method returns last index of the given
character value or substring. If it is not found, it returns -1
class Main{
public static void main(String args[]){
String s1="this is index of example";
int index1=s1.lastIndexOf('s');
System.out.println(index1);
}
}
replaceAll() - Example
The java string replaceAll() method returns a string replacing all the
sequence of characters matching regex and replacement string.
class Main{
public static void main(String args[]){
String s1="codemithra is a very good learning platform";
String replaceString=s1.replaceAll("a","e");
System.out.println(replaceString);
}
}
toLowerCase() - Example
The java string toLowerCase() method returns the string in lowercase letter
class Main{
public static void main(String args[]){
String s1="ETHNUS HELLO guYS";
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}
}
toUpperCase() - Example
class Main{
public static void main(String args[]){
String s1="hello guys";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}
}
trim() - Example
The java string trim() method eliminates leading and trailing spaces.
The unicode value of space character is '\u0020'.
class Main {
public static void main(String[] args) {
double d = 858.48;
String s = Double.toString(d);
int dot = s.indexOf('.');
System.out.println(dot + " digits " + "before decimal point.");
System.out.println( (s.length() - dot - 1) + " digits after
decimal point.");
}
}
StringBuilder Constructor
Constructor Description
creates an empty string Builder with the initial
StringBuilder() capacity of 16.
StringBuilder(String str) creates a string Builder with the specified string.
creates an empty string Builder with the specified
StringBuilder(int length) capacity as length.
StringBuilder Methods
Method Description
is used to append the specified string with this string.
The append() method is overloaded like append(char),
append(boolean), append(int), append(float),
public StringBuilder append(String s) append(double) etc.
is used to insert the specified string with this string at
the specified position. The insert() method is
public StringBuilder insert(int offset, overloaded like insert(int, char), insert(int, boolean),
String s) insert(int, int), insert(int, float), insert(int, double) etc.
public StringBuilder replace(int is used to replace the string from specified startIndex
startIndex, int endIndex, String str) and endIndex.
public StringBuilder delete(int is used to delete the string from specified startIndex
startIndex, int endIndex) and endIndex.
public StringBuilder reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
StringBuilder Methods
Method Description
public void ensureCapacity(int is used to ensure the capacity at least equal to the given
minimumCapacity) minimum.
public char charAt(int index) is used to return the character at the specified position.
is used to return the length of the string i.e. total number of
public int length() characters.
public String substring(int
beginIndex) is used to return the substring from the specified beginIndex.
public String substring(int is used to return the substring from the specified beginIndex
beginIndex, int endIndex) and endIndex.
StringBuilder append() - Example
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");
System.out.println(sb);
}
}
StringBuilder insert() - Example
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java");
System.out.println(sb);
}
}
StringBuilder replace() - Example
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
}
}
StringBuilder delete() - Example
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.delete(1,3);
System.out.println(sb);
}
}
StringBuilder reverse() - Example
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.reverse();
System.out.println(sb);
}
}
StringBuilder capacity() - Example
The capacity() method of StringBuilder class returns the
current capacity of the Builder. The default capacity of the
Builder is 16.
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());
sb.append("Hello");
System.out.println(sb.capacity());
sb.append("java is my favourite language");
System.out.println(sb.capacity());
}
}
StringBuilder ensureCapacity() -
Example
The ensureCapacity() method of StringBuilder 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
class Main{
public static void main(String args[]){
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());
sb.append("Hello");
System.out.println(sb.capacity());
sb.append("java is my favourite language");
System.out.println(sb.capacity());
sb.ensureCapacity(10);
System.out.println(sb.capacity());
sb.ensureCapacity(50);
System.out.println(sb.capacity());
} }
String VS Stringbuilder
String StringBuffer
String class is immutable. StringBuffer class is mutable.
String is slow and consumes more memory when
you concat too many strings because every time StringBuffer is fast and consumes less
it creates new instance. memory when you concat strings.
String class overrides the equals() method of
Object class. So you can compare the contents of StringBuffer class doesn't override the
two strings by equals() method. equals() method of Object class.