×

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration of these usages is the regular expression package java.util.regex.

String: We can define a string as a series of chars. As we have discussed earlier, in Java, String objects are unchangeable. It is a constant and cannot be modified once it is built.

StringBuffer: This can be defined as a peer class for String that gives the most components. The string represents an immutable fixed-length string, and StringBuffer represents an extensible and writable string.

Syntax:

StringBuffer name = new StringBuffer("StringTutorial");

StringBuilder: Java's StringBuilder represents a variable sequence of characters. Because the Java String class builds an unchangeable string, the StringBuilderclass gives a substitute to the String class for creating mutable strings.

Syntax:

StringBuilder abc = new StringBuilder();
abc.append("NAME");

StringTokenizer: We utilize this class to tokenize a string. The StringTokenizer object internally maintains its current position within the tokenized string. A few steps push on this present position beyond the processed chars. Tokens are returned with the help of the substring of the string utilized to build the StringTokenizer object.

StringJoiner: This can be defined as a class in java.util package that is utilized to create a series of characters (strings) divided by delimiters. Optionally, it starts with the specified prefix and ends with the specified suffix. We can also do it by utilizing the StringBuilder class to add a delimiter after each string, but StringJoiner gives a simple method to do this instead of writing a lot of code.

Syntax:

public StringJoiner(CharSequence delimiter)

Java String Example

public class First{    
public static void main(String args[]) {    
String name="This"; //defined string by Java string literal    
char ch[]={'i','s'};    
String name1=new String(ch); //converts char array to string    
String name2=new String("Tutorial"); //Java string by new keyword    
System.out.println(name);    
System.out.println(name1);    
System.out.println(name2);    
   }
}    

Output:

This
is
Tutorial

We can analyse the above code and converts a char array into a String object. Then it shows the String objects name, name1, and name2 on the console with the help of the println() method.

Java String class methods

It provides several important methods to help execute operations on a sequence of char values.

No.MethodDescription
1char charAt(int index)It will result the character value of the certain index.
2int length()It will result the length of the string.
3static String format(String format, Object_args)This method gives a formatted string.
4static String format(Locale l, String format, Object_args)This method will also give a formatted string in the particular locale.
5String substring(int beginIndex)Results in a substring of the particular starting index.
6String substring(int beginIndex, int endIndex)This method will result into the particular starting and end index substring.
7boolean contains(CharSequence s)True or false as the output after comparing with a sequence of character values.
8static String join(CharSequence delimiter, CharSequence... elements)This method gives the combined string.
9static String join()This also gives the combined string.
10boolean equals(Object another)This method will check whether the particular object and the string are equivalent to each other.
11boolean isEmpty()This method looks in for the null string.
12String concat(String str)Concatenation of the particular string is performed.
13String replace(char old, char new)This method will replace every incident of the particular char value.
14String replace(CharSequence old, CharSequence new)This will also replace every incident of the particular CharSequence.
15static String equalsIgnoreCase(String another)This method will compare several strings. Uppercase/lowercase is not considered.
16String[] split(String regex)It will result into a split string that is equivalent to the regular expression.
17String[] split(String regex, int limit)This method will also result into a split string that is equivalent to the regular expression and the limit.
18String intern()This method provides the internal string.
19int indexOf(int ch)It will result the index of the particular char value.
20int indexOf(int ch, int fromIndex)This method will provide the particular char-valued position starting at the mentioned index.
21int indexOf(String substring)Results into the particular substring.
22int indexOf(String substring, int fromIndex)This method will provide the particular substring index starting at the specified index.
23String toLowerCase()Utilized to get the lowercase string.
24String toLowerCase(Locale l)It gives the small letters string with the help of the particular locale.
25String toUpperCase()It will return the input string in capital letters.
26String toUpperCase(Locale l)It gives the uppercase string with the help of the particular locale.
27String trim()Remove leading and trailing spaces from this string.
28static String valueOf(int value)This will convert the particular kind to a string. valueOf() is an overloaded method.

Related Topics

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.