Java Tutorial for Beginners – Strings in Java

String Methods
Source: Google

A String is a collection of characters in a sequence or array. In our blog Data types, we covered the fundamentals of Strings in Java. String is not a primitive data type in Java; instead, it is an object of the String class.

Strings are one of the most common and widely used classes in the language. A String is an array of characters, and because arrays are immutable by nature, a String is an immutable object, meaning it is constant and cannot be changed after it is created.

Strings in Java

A string is a collection of characters. You can work with character data in Java in four different ways: You can work with a single character or a group of characters in four different ways:

  • String class
  • Character Class
  • String Buffer Class
  • String Builder Class

Creating String in Java Language

String name = "Welcome  to String Tutorial";

We can also create Strings by new keyboard of string constructor.

String name = new String("Welcome to String Tutorial");

Methods of String classes

Accessor methods are methods in the String class that are used to obtain information about objects. Many accessor methods are provided by the String class, which are used to perform operations on strings. With the help of examples and code, we’ll go over some of the most important methods of the String class in depth.

Java String Methods

Code to illustrate Java Strings


import java.util.*;
public class Strings {
    public static void main(String [] args){
        String myName ="Abhinav Pandey";
        String myMiddle = new String("Abhinav Pandey");
        String aM = "Abhinav,abhi,adarsh,mom,dad";
        String aW [] = aM.split(",");
        for(String as : aW){
            System.out.println(as);
        }

        System.out.println(myName);
        System.out.println(myName.charAt(5));
        System.out.println(myName.length());
        System.out.println(myName.substring(5));
        System.out.println(myName.substring(4,8));
        System.out.println(myName.equals(myMiddle));
        System.out.println(myName.isEmpty());
        System.out.println(myName.concat(myMiddle));
        System.out.println(myName.replace('a','A'));
        System.out.println(myName.indexOf('y'));
        System.out.println(myName.toLowerCase(Locale.ROOT));
        System.out.println(myName.toUpperCase(Locale.ROOT));
        System.out.println(myName.trim());
    }
}

OUTPUT:

Abhinav
abhi
adarsh
mom
dad
Abhinav Pandey
a
14
av Pandey
nav 
true
false
Abhinav PandeyAbhinav Pandey
AbhinAv PAndey
13
abhinav pandey
ABHINAV PANDEY
Abhinav Pandey

Process finished with exit code 0

Summary

In Java programming, strings are extremely important. The String class in Java allows you to manipulate and perform operations on strings. We looked at the String class from the java.lang package in this article.

We also went over the most important methods for dealing with Strings, as well as their syntax and sample code. This article will guide you through the process of writing Java programs that are related to Strings.

Also visit the other blogs too: https://mechomotive.com/java-tutorials-for-beginners/

Thank You for reading this blog !!!!