Java String Complete Tutorial

In this section we will learn what the String class is and how to use it in Java.

What is String in Java?

String is a data type (a class) that can be used to create variables capable of storing string of characters.

How to Declare String in Java:

In Java there are two ways of creating an object of type String:

– String Literal: This is covered in the string literal section.

– String Class

Java String Class

A mentioned before, String is a class and just like many other classes, this one also has constructors that can be used to create an object of this type.

Example: declaring a String via String constructor

String st1 = new String ("John");

Here we used the constructor of the String class that accepts a string literal.

Note: when we use double quotation ” ” around one or more characters, we’re creating a string literal. Also the double quotation can be used without a character in it which in that case it is called an empty string literal.

Check the string literals section for more details.

When we create an object via the new keyword that object will be stored in the heap memory and its memory address (reference) will be stored in the st1 variable.

So basically the st1 only has a reference to the object and not the object itself.

Java String Methods (AKA Java String Functions)

The String class has multiple useful methods that we can use for different purposes:

  • To get the number of characters in the target string.
  • To get part of the target string (AKA substring).
  • To attach a new string.
  • To get the index number of a specific character.
  • Etc. 

Java String Methods List

In the table below you can see the list of string related methods. We will cover them in the next couple of sections but for now you can see an introductory description about each of these methods:

MethodDescriptionReturn Type
charAt()This method takes an integer value as the index number and returns the character in that index.char
chars()The Java String chars() method is used to return a stream of code points for characters in a string value.Stream
codePointAt()When we want to get the Unicode of a character in a specific index, we use this method.int
codePointBefore()  Just like the codePointAt() method, this one also returns the Unicode of the character but of the one that is before the index number specified as the argument to the method.int
codePointCount()This method returns the number of Unicode in the range that we set as the argument of the method.int
compareTo()This method is used to compare two string values lexicographically (character by character).int
compareToIgnoreCase()The method is used to compare two string values just like the compareTo() method but without considering the case differences.int
concat()This method is used to concatenate two string values together.String
contains()The method is used to check if the target string has the specified sequence of characters (That we specify as the argument)boolean
contentEquals()This method is used to check if the character sequence that we set as the argument of the method, is the same as the content of the target string itself.boolean
copyValueOf()This method takes a character array and it will return a string representation of that array.String
endsWith()Via this method we can check and see if the target String ends with the specified sequence of charactersboolean
equals()Via this method we can compare two string objects based on their content.boolean
equalsIgnoreCase()This method is used to compare two string values together, ignoring the case considerations.boolean
format()This method is used to return a formatted string.String
getBytes()Via this method we can return the array of bytes that represents the string.byte[]
getChars()This method turns the string content into characters and put it in the target char array that we set as its argument.void
hashCode()This method returns the hash code of a stringint
indexOf()This method takes a character sequence and returns the index of the first occurrence in the target string.int
indent()The Java String indent() method is used to add indentation at the beginning of a string value.String
intern()This method is used to store a copy of a String object that is created via the new keyword into the string pool and returns a reference of the pooled object.String
isEmpty()Via this method we can check and see if the string is empty or not.boolean
isBlank()The Java String isBlank() method is used to check a string value and see if it’s empty or contains only white spacesboolean
lastIndexOf()This method is used to find the index number of the last occurrence of the specified sequence of characters.int
length()via this method we can get the length (number of characters) of a String.int
lines()The Java String lines() method is used to create a stream of lines from a string value.Stream
matches()This method takes a regular expression and checks to see if the string matches this expression.boolean
regionMatches()This method is used to check if parts of two strings matches.boolean
replace()This method is used to search for a character and replace it with another character and returns the new string.String
replaceFirst()This method takes two arguments. The first one is a regular expression and the second one is a string. The method will match the regular expression against the target String object and the first match will be replaced by the string that we set as the second argument of the method.String
replaceAll()Just like replaceFirst() method, this one also works the same way. The difference is that, this method will replace all the matches with the string value that we set as the second argument of the method.String
repeat()This method is used to repeat the value of a String object multiple times and return the result, as a new String object.String
split()This method is used to split a string into an array of substringsString[]
strip()The Java String strip() method is used to remove any white space from the leading and trailing parts of a string value.String
stripLeading()The Java String stripLeading() method is used to remove any white space from the leading part (start) of a string value.String
stringTrailing()The Java String stripTrailing() method is used to remove any white space from the trailing part (end) of a string value.String
startsWith()This method is used to check if the string object starts with the specified characters.boolean
substring()This method is used to take a portion of a string and return it as a new String-object.String
toCharArray()Via this method we can convert a string into character array.char[]
toString()via this method we can get the value that is stored in a String objectString
toUpperCase()This method is used to convert all the characters of a String into uppercase letters and return a new String-object.String
trim()Via this method we can remove whitespaces from both sides of a stringString
toLowerCase()This method is used to convert a string into lowercase lettersString
valueOf()This method is used to convert different data types into String object.String
Java String methods

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies