Professional Documents
Culture Documents
Strings
Output :
java
strings
example
Some important operations of strings
▪ String Concatenation
▪ String Comparison
▪ Substring
▪ Length of String
etc.
String
Concatenation
▪ There are 2 methods to concatenate two or more string.
Using Using +
concat() operator
method
Concatenate
two or more
string.
String Comparison
▪ String comparison can be done in 3 ways.
Using ==
operator
Using By
equals() CompareTo(
) method
method
String
Comparison
String Comparison Example
Substring in Java
▪ A part of string is called substring. In other words, substring is a subset of another string.
▪ We can get substring from the given string object by one of the two methods:
1. public String substring(int startIndex):
2. public String substring(int startIndex, int endIndex):
Length of String
▪ The java string length() method finds the length of the string. It returns count of total
number of characters.
Some more string class methods
▪ The java.lang.String class provides a lot of methods to work on string. By the help of
these methods, we can perform operations on strings.
▪ Here we will learn about the following methods :
1. charAt()
2. contains()
3. getChars()
4. indexOf()
5. replace()
6. toCharArray()
7. toLowerCase()
8. toUpperCase()
charAt() &
contains()
charAt() = returns a char value at the given index number. The index number starts from 0.
contains() = searches the sequence of characters in this string. It returns true if sequence of char
values are found in this string otherwise returns false.
getChars()
getChars() = copies the content of this string into specified char array. There are 4
arguments passed in getChars() method. It throws
StringIndexOutOfBoundsException if beginIndex is greater than endIndex.
indexOf()
indexOf() = returns index
of given character value
or substring. If it is not
found, it returns -1. The
index counter starts from
zero.
replace()
replace() = returns a string replacing all the old char or CharSequence to new char or
CharSequence.
toCharArray()
2) String is slow and consumes more memory when StringBuffer is fast and consumes less memory
you concat too many strings because every time when you cancat strings.
it creates new instance.
3) String class overrides the equals() method of StringBuffer class doesn't override the equals()
Object class. So you can compare the contents of method of Object class.
two strings by equals() method.
Difference between StringBuffer and StringBuilder