×

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

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.