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

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 …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More