Awesome explanation of Strings in Java
What do you mean by String?
Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
Creating Strings
The normal way to create a string in Java is to simply write:-
String s = “Hello World”;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way to create a string is:-
String s = new String (“Hello World”);
String Buffer:-
StringBuffer is a String companion class that provides a lot of the same functionality as strings. StringBuffer shows growable and writable character sequences, whereas string shows fastened-length, unchangeable character sequences.
Syntax:
StringBuffer s = new StringBuffer("Hello World");
String Tokenizer:-
The StringTokenizer class in Java is used to tokenize a string.
String Joiner:-
String Joiner is a java.util package class that is used to build a sequence of characters divided by a delimiter and optionally starting with a transferred prefix and finishing with a transferred suffix.
Syntax:
public StringJoiner(CharSequence delimiter);
As we study in the above part, we have sufficient knowledge about replacing a character in a string
So, there are three ways to replace a character in a string:
StringBuilder:
In contrast to the String Class, the StringBuilder class includes a built-in function for this — setCharAt (). By calling this function and giving the character and the index as parameters, you can replace the character at a given index.
Syntax:
StringBuilder str = new StringBuilder();
str.append("GFG");
Null and empty string in Java:-
NULL STRING :- A string that contains no value in it, simply called a Null String.
We can use the null string in Java in the following way:
String a = null;
“a” is a null string in this case. The attribute does not relate to any memory location in a heap when the null is assigned to the string variable.
EMPTY STRING:- An empty string is a type of string with no characters and a very well-defined length of 0. On an empty string, we can do all of the string operations.
Blank String
If we additionally want to identify blank strings, we may use String#trim to accomplish this. Before performing the check, it will remove any leading and trailing whitespaces.
We can use an empty string in Java in the following way:
String x = "";
“x” is an empty string in this case. The empty string expresses that the attribute corresponds to a memory location of a string in a clump when we delegate it to the string variable.
Different ways to find a string is null or empty:
- String length method:-
The Empty () method refers to the String class's length () method. Because the is Empty () method isn't accessible in Java 6 and below, we can use the length () method instead:
public class IsEmpty{
public static void main(String args[]){
String s1=””;
String s2=”helloworld”;
String s3=”star”;
System.out.println(s1.isEmpty()); // Command to print
System.out.println(s2.isEmpty());
System.out.println(s3.isEmpty());
}}
// IT RETURNS TRUE IF LENGTH IS 0 OTHERWISE IT RETURNS FALSE
Output:
True
False
False
- String Equals Method:
class Main
{
private static String EMPTY = ""; // Empty class
public static void main(String[] args)
{
String str = ""; //Empty Class
System.out.println(str == null || str.equals(EMPTY));
System.out.println(str == null || EMPTY.equals(str));
}
}
Output
True
True
- Guava Library Method:
The static utility function isNullOrEmpty(String) in Guava's Strings class gives true if the provided string is null or empty.
class Main
{
public static void main(String[] args)
{
System.out.println(Strings.isNullOrEmpty("")); //empty class
System.out.println(Strings.isNullOrEmpty(null)); //null class
System.out.println(Strings.isNullOrEmpty("Hello"));
}
}
Output
True
True
False
Empty Method:
The String.isEmpty() function is recommended for checking an empty string in Java starting with Java 7. If the string is null, a null verification should come before the method call to avoid a NullPointerException.
class Main{
public static void main(String[] args)
{
String str = ""; //Empty string
System.out.println(str == null || str.isEmpty());
}
}
OUTPUT:
True
Apache:
StringUtils.isEmpty(String) in the Apache Commons Lang library tests whether a string is empty or null. It also has a function called StringUtils.isBlank(String), which looks for whitespace.
class Main{
public static void main(String[] args)
{
System.out.println(StringUtils.isEmpty("")); // Empty
System.out.println(StringUtils.isEmpty(null)); // Null
System.out.println(StringUtils.isEmpty("Java")); // String
System.out.println(StringUtils.isBlank(" ")); // Blank
}
}
OUTPUT:
True
True
False
True