String Class And
StringBuffer
Class,Formatting String data
using format()
Method
STRING CLASS
The use of strings is essential to programming. Java has a String class that allows
for the creation and manipulation of strings. The character sequence can also be
represented using an interface called CharSequence. One of the classes that
implements this interface is the String class. Thus, in Java, a string is an object that
is essentially a sequence of characters.
For instance, the word "hello" is made up of a string of the characters "h," "e," "l,"
"l," and "o." Additionally, we have a variety of String methods that make it simple
to interact with String in Java.
What is String Class in Java?
String refers to a group of characters. String objects in Java are immutable, which
simply means they can never be modified after they have been created. Java only
supports operator overloading for the String class. The + operator allows us to
combine two strings. For
instance, "a"+"b"="ab." StringBuilder and StringBuffer are two helpful classes in
Java that can be used to manipulate strings.
How to Create a String Object in Java?
A few of the more well-known techniques to generate a string object in Java are
listed below.
By String Literal
The most typical method of producing string is this one. In this instance, double
quotes are used to surround a string literal.
String str = "abc";
When double quotes are used to generate a String, JVM searches the String pool(A
location inside the heap memory is called the String Constant Pool. It includes
distinctive strings. The JVM determines whether a string is already present in the
string pool before generating a new object in the pool. ) to see whether any other
Strings with the same value are already stored there. If an existing String object is
found, it simply returns the reference to it; otherwise, a new String object with the
specified value is created and stored in the String pool.
Using New Keyword
Like any other Java class, we can construct String objects with the new operator.
The String class has a number of constructors that can be used to create a String
from a char array, byte array, StringBuffer, or StringBuilder.
String str = new String("abc");
char[] a = {'a', 'b', 'c'};
String str2 = new String(a);
Constructors of String Class in Java
1. String()
produces a blank string. Due to the immutability of String, it is largely worthless.
2. String(String original)
from another string, generates a string object. String is useless since it is
immutable.
3. String(byte[] bytes)
creates a new string using the system's default encoding from the byte array.
4. String(byte bytes[], String charsetName)
utilises the character encoding supplied. UnsupportedEncodingException is
thrown if the encoding is not supported.
5. String(byte[] byte_arr, Charset char_set)
In order to create a new String, decode the byte array. It decodes using the char set.
6. String(byte bytes[], int offset, int length)
The first byte to be decoded is indicated by the offset. The length defines how
many bytes need to be decoded. If offset, length, or offset is more than bytes, this
constructor raises an IndexOutOfBoundsException.
7. String(byte bytes[], int offset, int length, Charset charset)
Similar to the constructor above, the only difference is that the encoding to be used
must be specified.
8. String(byte bytes[], int offset, int length, String charsetName)
The character set encoding name is supplied as a string, unlike the example before.
If the encoding is not supported, this will throw
an UnsupportedEncodingException.
9. String(char value[])
uses the character array to generate the string object.
10. String(char value[], int offset, int count)
uses the character array to generate the string object.
11. String(char value[], int offset, int count)
The initial character's index is specified by the offset. The amount of characters to
use is determined on the length. If offset, length, or offset and value are negative,
this constructor raises an IndexOutOfBoundsException.
12. String(int[] codePoints, int offset, int count)
Creates a string from an array of Unicode code points as input. If any of the code
points are incorrect, it raises the IllegalArgumentException exception. If the
offset length, or offset is more than codePoints, this constructor raises an
IndexOutOfBoundsException.
13. String(StringBuffer buffer)
uses the data contained in the string buffer to produce a new string. Internally, this
constructor uses the StringBuffer function toString()
14. String(StringBuilder buffer)
Takes the contents of the string builder and produces a new string.
Methods of String Class in Java
We will now talk about Java's methods for Strings. String Methods are highly
helpful since they make working with Strings in Java very simple. The String
methods in Java allow us to do many different things, including determining a
string's length, concatenating several strings, determining whether two strings are
equal, changing a string's characters' case, and much more. Let's talk about a few
of Java's most well-liked and frequently utilised String methods. It should be noted
that the Java class java.lang.String offers all of these string methods.
Method Descr
char charAt(int index) It provides a char value
int length() return the len
static String format(String format, Object... args) returns a form
It gives back a formatte
static String format(Locale l, String format, Object... args)
loca
String substring(int beginIndex) It provides a substring b
String substring(int beginIndex, int endIndex) For the supplied begin index and
finish index, a substring is returned.
After comparing the seq
boolean contains(CharSequence s)
returns tru
Method Descr
static String join(CharSequence delimiter, CharSequence... elements) It returns a j
static String join(CharSequence delimiter, Iterable<? extends CharSequence>
It returns a j
elements)
It determines whether th
boolean equals(Object another)
object ar
boolean isEmpty() check if string i
String concat(String str) It concatenates the string
String replace(char old, char new) It replaces the old ch
It changes the supplied C
String replace(CharSequence old, CharSequence new)
appe
static String equalsIgnoreCase(String another) It compares a different str
String[] split(String regex) A split string that match
It gives back a split strin
String[] split(String regex, int limit)
reg
String intern() An interned str
int indexOf(int ch) The requested char va
Starting with the suppli
int indexOf(int ch, int fromIndex)
specified char
int indexOf(String substring) The requested substri
The required substring i
int indexOf(String substring, int fromIndex)
supplied inde
String toLowerCase() returns string
String toLowerCase(Locale l)
String toUpperCase() returns string
IIt gives back an uppercas
String toUpperCase(Locale l)
loca
String trim() removes leading and
returns a string represent
static String valueOf(int value)
val
STRING BUFFERCLASS