7 Strings
7 Strings
7 Strings
Method Description
char charAt(int index) returns char value for the particular index
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 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'.
String s2 = “hello”;
String s3 = “Hello”;
String objects will be created in String
Constant Pool area when using
literals
Scenario 1
Scenario 3
String s1 = “Hello”;
System.out. println(sb);
StringBuilder Constructor
Constructor Description
creates an empty string Builder with the initial
StringBuilder() capacity of 16.
creates a string Builder with the specified
StringBuilder(String str) string.
creates an empty string Builder with the
StringBuilder(int length) specified capacity as length.
StringBuilder Methods
Method Description
is used to append the specified string with this string. The append()
public StringBuilder append(String s) method is overloaded like append(char), append(boolean),
append(int), append(float), append(double) etc.
is used to insert the specified string with this string at the specified
position. The insert() method is overloaded like insert(int, char),
public StringBuilder insert(int offset, String s)
insert(int, boolean), insert(int, int), insert(int, float), insert(int,
double) etc.
public StringBuilder replace(int startIndex, int is used to replace the string from specified startIndex and
endIndex, String str) endIndex.
public StringBuilder delete(int startIndex, int
endIndex) is used to delete the string from specified startIndex 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 Buffer & Mutable nature
System.out. println(sb);
String Buffer constructors
Constructor Description
creates an empty string buffer with the initial
StringBuffer() capacity of 16.
creates a string buffer with the specified
StringBuffer(String str) string.
StringBuffer(int creates an empty string buffer with the
capacity) specified capacity as length.
StringBuffer 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 StringBuffer append(String s) append(double) etc.
is used to insert the specified string with this string at the
specified position. The insert() method is overloaded like
insert(int, char), insert(int, boolean), insert(int, int), insert(int,
public StringBuffer insert(int offset, String s) float), insert(int, double) etc.
public StringBuffer replace(int startIndex, is used to replace the string from specified startIndex and
int endIndex, String str) endIndex.
public StringBuffer delete(int startIndex, int is used to delete the string from specified startIndex and
endIndex) endIndex.
public StringBuffer reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
String Buffer 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
public int length() of characters.
public String substring(int is used to return the substring from the specified
beginIndex) beginIndex.
public String substring(int is used to return the substring from the specified
beginIndex, int endIndex) beginIndex and endIndex.
String VS Stringbuilder
A. Stack
B. Heap
C. Data Segment
D. Register
Question: 02
class Main{
public static void main(String args[]){
String str1 = "Hello";
String str2 = "Hello"; A. True False True
String s1 = new String("Hello"); B. True False False
String s2 = new String("Hello");
C. False True False
System.out.println(str1 == str2);
System.out.println(str2 == s1); D. False False True
System.out.println(s1 == s2);
}
}
Question: 04
class Main{
public static void main(String args[]){
String s1 = null;
System.out.println(s1 !=null ?
A. null
"Hai this string is not empty" : s1);
B. Hai this string is not empty
}
C. Compilation Error
}
D. Runtime Error
Question: 06
class Main
{
public static void main(String[] args){
String s = "Java"+1+2+"Quiz"+""+(3+4); A. Java3Quiz34
System.out.println(s); B. Java12Quiz34
}
C. Java3Quiz7
}
D. Java12Quiz7
Question: 07
class Main
{
public static void main(String[] args){
String x = "abc"; A. Null
String y = "abc"; B. abcabc
x.concat(y);
C. abc
System.out.print(x);
} D. Compilation error
}
Question: 08
A. String class
B. Object class
C. String Buffered class
D. None of these
Question: 09
A. StringBuffer
B. StringBuilder
C. Both a and b
D. None of these
Question: 11
A. java.util
B. java.lang
C. ArrayList
D. None of the mentioned
Question: 13
A. get()
B. Sizeof()
C. lengthof()
D. length()
Question: 14
A. CHARAT()
B. charat()
C. charAt()
D. ChatAt()
Question: 15
A. equals()
B. Equals()
C. isequal()
D. Isequal()
Question: 16
A. char
B. int
C. boolean
D. All of the mentioned
Question: 17
A. startsWith()
B. trim()
C. Trim()
D. doTrim()
Question: 19
A. substring()
B. Substring()
C. SubString()
D. None of the mentioned
Question: 21
import java.util.*;
public class Main
{ A. abc
public static void main(String[] args) { B. abc
StringTokenizer stuff = new StringTokenizer(
+def
"abc+def+ghi", "+", true );
C. abc
System.out.println( stuff.nextToken() );
System.out.println( stuff.nextToken() ); def
} D. abc
} +
Question: 30
class Main
{
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68}; A. ABC
String s = new String(ascii, 1, 3); B. BCD
System.out.println(s); C. CDA
} D. ABCD
}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://learn.codemithra.com