Strings Class
Strings Class
Java
String String Pool
String methods StringBuffer
String VS StringBuffer Vs
StringBuilder StringBuilder
Regular Expression
String
Two strings in C++ will evaluate true to a “==” operation if they contain the same
value, regardless of whether they are the same object or not.
In Java comparing two String variables with ‘==’ does a reference equality test.
Surprisingly you might declare the same two Strings and initialize them with the
same string literal — then you’d expect them not to have reference equality —
but they might still because of string interning.
String
There are two ways to create String object:
1.By string literal
2.By new keyword
1) String Literal
For example:
String s1="Welcome";
String s2="Welcome";//
It doesn't create a new instance
String
2) By new keyword
String s=new String("Welcome"); //creates two objects and one reference variable
In such case, JVM will create a new string object in normal (non-pool) heap
memory, and the literal "Welcome" will be placed in the string constant pool. The
variable s will refer to the object in a heap (non-pool).
String Pool
String methods
Method Description Return Type
charAt() Returns the character at the specified index char
(position)
codePointAt() Returns the Unicode of the character at the int
specified index
codePointBefo Returns the Unicode of the character before the int
re() specified index
codePointCou Returns the number of Unicode values found in a int
nt() string.
compareTo() Compares two strings lexicographically int
compareToIgn Compares two strings lexicographically, ignoring int
oreCase() case differences
String methods
Method Description Return Type
concat() Appends a string to the end of another string String
contains() Checks whether a string contains a sequence of boolean
characters
contentEquals Checks whether a string contains the exact same boolean
() sequence of characters of the specified
CharSequence or StringBuffer
copyValueOf() Returns a String that represents the characters of String
the character array
endsWith() Checks whether a string ends with the specified boolean
character(s)
equals() Compares two strings. Returns true if the strings boolean
are equal, and false if not
String methods
Method Description Return Type
equalsIgnoreC Compares two strings, ignoring case considerations boolean
ase()
format() Returns a formatted string using the specified String
locale, format string, and arguments
getBytes() Encodes this String into a sequence of bytes using byte[]
the named charset, storing the result into a new
byte array
getChars() Copies characters from a string to an array of chars void
hashCode() Returns the hash code of a string int
indexOf() Returns the position of the first found occurrence int
of specified characters in a string
String methods
Method Description Return Type
intern() Returns the canonical representation for the String
string object
isEmpty() Checks whether a string is empty or not boolean
lastIndexOf() Returns the position of the last found int
occurrence of specified characters in a string
length() Returns the length of a specified string int
matches() Searches a string for a match against a regular boolean
expression, and returns the matches
offsetByCodePoints Returns the index within this String that is int
() offset from the given index by codePointOffset
code points
regionMatches() Tests if two string regions are equal boolean
String methods
Method Description Return Type
replace() Searches a string for a specified value, and returns a String
new string where the specified values are replaced
Constructor Description
StringBuffer() It creates an empty String buffer with the
initial capacity of 16.
capacity() the total allocated capacity can be found by the capacity( ) method
charAt() This method returns the char value in this sequence at the specified index.
replace() Replace one set of characters with another set inside a StringBuffer object
StringBuilder
Since the String Class in Java creates an immutable sequence of characters, the
StringBuilder class provides an alternative to String Class, as it creates a
mutable sequence of characters.
However, the StringBuilder class differs from the StringBuffer class on the
basis of synchronization. The StringBuilder class provides no guarantee of
synchronization whereas the StringBuffer class does.
StringBuilder
Important constructors of StringBuilder class:
Constructor Description
StringBuilder() It creates an empty String Builder with the
initial capacity of 16.
public StringBuilder It is used to append the specified string with this string. The append() method is
append(String s) overloaded like append(char), append(boolean), append(int), append(float),
append(double) etc.
public StringBuilder It is used to insert the specified string with this string at the specified position.
insert(int offset, String s) The insert() method is overloaded like insert(int, char), insert(int, boolean),
insert(int, int), insert(int, float), insert(int, double) etc.
public StringBuilder
replace(int startIndex, int It is used to replace the string from specified startIndex and endIndex.
endIndex, String str)
public StringBuilder
delete(int startIndex, int It is used to delete the string from specified startIndex and endIndex.
endIndex)
public StringBuilder
reverse() It is used to reverse the string.
StringBuilder
Methods of StringBuilder class:
public void
ensureCapacity(int It is used to ensure the capacity at least equal to the given minimum.
minimumCapacity)
public int length() It is used to return the length of the string i.e. total number of characters.
public String substring(int
beginIndex) It is used to return the substring from the specified beginIndex.
Email validation and passwords are a few areas of strings where Regex is widely used to define
the constraints.
Example:
/* Following line prints "true" because the whole text "geeksforgeeks" matches
pattern "geeksforge*ks"
*/
System.out.println(Pattern.matches("geeksforge*ks", "geeksforgeeks"));
Regular Expression
Matcher Class:
This object is used to perform match operations for an input string in java, thus
interpreting the previously explained patterns. This too defines no public constructors.
This can be implemented by invoking a matcher() on any pattern object.
Example:
// Create a pattern to be searched Custom pattern
Pattern pattern = Pattern.compile("geeks");
Expression Description
[abc] Find one character from the options between the
brackets
[^abc] Find one character NOT between the brackets
[0-9] Find one character from the range 0 to 9
Regular Expression
Metacharacters
Metacharacters are characters with a special meaning:
Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx
Regular Expression
Quantifiers
Quantifiers define quantities:
Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
Anirudha Gaikwad