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 of string with object in Java:


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.


Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More