Java – Strings3 min read

A string is defined as a series of characters or an array of characters in java. They are present within the double quote.

Create a String:

One way to create a String is to directly declare it within the double quote that is by String Literal:

Like any other object, it is possible to create a string object with the help of a new keyword.
such as:

Example:

Output:


Example of string with object in Java:

Output:

Note that: it also takes blank space as a character.
Also, string objects are immutable that is their values are unchangeable once the object is created
.


String Methods:

Java provides Various String Methods for String Operation such as compare(), concat(), equals(), length(), compareTo() and amany more.

List of some string Methods supported by Java are:

  1. char charAt(int index):
    This method returns the character at the specified index.
  2. int compareTo(String secondString):
    The method compares the two strings depending upon their Unicode Value.
  3. int compareTo(Object o):
    Compares the present String to another object.
  4. String concat(String str):
    This method concatenates the String ‘str’ at the end of the string.
  5. boolean equals(Object obj):
    This method compares the string to the specified object.
  6. boolean endsWith(String suffix):
    This method checks or tests whether the string ends with the specified suffix or not.
  7. boolean startsWith(String prefix):
    It checks for the string to have a specified prefix and depending on that it returns a true or false boolean value.
  8. int compareToIgnoreCase(String str):
    It compares the string the same as compareTo() but here it ignores the case of the string.
  9. int hashCode():
    This method returns the hash code of the string.
  10. int indexOf(int ch):
    The index of the first occurrence of the specified character ‘ch’ is returned in the string.
  11. int lastIndexOf(int ch):
    This one returns the last occurrence ‘ch’ in the string.
  12. int indexOf(String str):
    The index of the first occurrence of the specified substring ‘str’ is returned through this method.
  13. int lastindexOf(String str):
    similarly, this method returns the last occurrence of the string ‘str’.
  14. String intern():
    This method first searches for the particular string and returns the reference of it otherwise it allocates the memory to the particular string and assigns the reference to it.
  15. int length():
    This method returns the length of the string.
  16. char[] toCharArray():
    It is used to convert the string to a new array.
  17. String toString():
    This already present string is itself return.
  18. String toLowerCase():
    It is used to all the characters of the string to lower case by the default locale.
  19. String toUpperCase():
    It is used to all the characters of the string to upper case by the default locale.
  20. String trim():
    It returns the substring of the original string after omitting leading and trailing white spaces.

There are many more String methods such as, static String copyValueOf(char[] data),  static String valueOf(),  byte[] getBytes(),  boolean matches(String regex),  static String copyValueOf(char[] data, int offset, int count),  string[] split(String regex) and so on.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …