0% found this document useful (0 votes)
23 views34 pages

Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I

soft skill material includes different topics related to basic programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views34 pages

Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I

soft skill material includes different topics related to basic programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

String and

Stringbuilder
What you’ll Learn

String
String Methods
Stringbuilder
String

In Java, string is basically an object that represents sequence of char


values.
An array of characters works same as Java string.
char[ ] ch = {'e’,'t','h','n','u','s'};
Syntax
<String_Type> <string_variable> = “<sequence_of_string>”;

String str="Hello!";
Ways to Create String

Using String literal


You can create String objects with String literal
String s1="Welcome";
String s2="Welcome"; //It doesn't create a new instance
Using new keyword
This is the common way to create a String object in java.
String str1= new String("Hello!");
Using character array:
You could also convert character array into String here
char ch[]={ 'H','e','l','l','o','!'};
Memory Allocation

String Object is created, two objects will be created


The Heap Area
The String constant pool
The String object reference always points to heap area object.
Example: String str = “Ethnus”;

0 1 2 3 4 5 6

E t h n u s \0

0x2345 0x2345 0x2345 0x2345 0x2345 0x2345 0x2345


2 3 4 5 6 7 8
String Methods

Method Description

char charAt(int index) returns char value for the particular index

int length() returns string length

String substring(int beginIndex) returns substring for given begin index.

boolean equals(Object another) checks the equality of string with the given object.

String concat(String str) concatenates the specified string.

String replace(char old, char new) replaces all occurrences of the specified char value.

int indexOf(int ch) returns the specified char value index.


charAt() - Example

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

The java string substring() method returns a part of the string

public class Main {


public static void main(String[] args) {
String s1="Ethnus and Codemithra";
String substr = s1.substring(0);// Starts with 0 and
goes to end
System.out.println(substr);
String substr2 = s1.substring(5,10);// Starts from 5
and goes to 10
System.out.println(substr2);
}
}
concat() - Example

The java string concat() method combines specified string

public class Main{


public static void main(String args[]){
String s1="Today you";
System.out.println(s1);
s1=s1.concat(" will learn Java String");
System.out.println(s1);
}
}
contains() - Example

The java string contains() method searches the sequence of characters


in this string

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 String equalsIgnoreCase() method compares the two given


strings on the basis of content of the string irrespective of case of the
string

public class Main{


public static void main(String args[]){
String s1="codemithra";
String s2="codemithra";
String s3="CODEMITHRA";
String s4="ethnus";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
}
}
indexOf()

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

public class Main{


public static void main(String args[]){
String s1="this is index of example";
int index1=s1.indexOf("is");
int index2=s1.indexOf("index");
System.out.println(index1+" "+index2);
int index3=s1.indexOf("is",4);
System.out.println(index3);
int index4=s1.indexOf('s');
System.out.println(index4);
}
}
indexOf(substring) - Example

This method takes substring as an argument and returns index of first


character of the substring.

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

The java string isEmpty() method checks if this string is empty or


not. It returns true, if length of string is 0 otherwise false

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

The java string toUpperCase() method returns the string in uppercase


letter.

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'.

public class Main{


public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"ethnus");
System.out.println(s1.trim()+"ethnus");
}
}
Number to String - Example

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.

You might also like