String Methods in Java


One of the most vital parts of Java is Strings. As a Java programmer, one of your main tools for storing and processing data is the String class.
What makes Strings so powerful in Java? The answer to that will be methods of String class. The java.lang.String provides a bunch of useful methods which can be used to perform various operations on a sequence of character values.
Let us list out all the methods which falls under the String class. You can just click on any method and read about it in detail with explained example codes.


String Methods

charAt(int index)
– Returns the character value at the specified index. The index value should be between 0 to length()-1, both inclusive.
codePointAt(int index)
– Returns the Unicode value of the character at the specified index.
codePointBefore(int index)
– Returns the Unicode value of the character before the specified index.
codePointCount(int beginIndex, int endIndex)
– Returns the number of Unicode point values in the specified range of the String.
compareTo(String anotherString)
– Compares two strings in dictionary order, also known as lexicographically.
concat(String anotherString)
– Concatenates the specified string to the end of this string.
conatins(CharSequence cs)
– Returns true or false after checking if the specified sequence of characters is present in the string or not.
contentEquals(CharSequence cs)
– Compares this string to the specified character sequence.
copyValueOf(char[ ] data)
– Returns a string that represents the character sequence in the array specified.
endsWith(String suffix)
– Checks whether the string ends with the specified characters or not.
equals(Object anobject)
– Returns true or false after comparing this string to the specified string.
indexOf(int ch)
– Returns the index within the string of the first occurrence of the specified character/substring.
length()
– Returns the length of the specified string.
lastIndexOf(int ch)
- Returns the index within the string of the last occurrence of the specified character/substring.
replace(char oldChar, char newChar)
– Returns a new string by replacing all the occurrences of oldChar with newChar.
startsWith(String prefix)
– Returns true of false after checking if the string starts with the specified prefix.
substring(int beginIndex)
– Returns a new string that is a substring of this string.
toLowerCase() and toUpperCase()
– Converts all the characters of the string to lower case and upper case respectively.

Summary

We have seen the overview of String methods in Java. Make sure to check each one of these methods in detail and get a fair idea about how it works. Some of the methods listed above have sub-categories depending upon the parameters specified.

Read More

Author : Satyam Kumar -



You will also like:


Snowflake Cloud Data Warehouse

What is Snowflake, and what can you do with Snowflake? In video article.

Learning Journal

Array Concatenation in Java

Learn how to join two or more arrays together in Java by various methods.

Learning Journal

What is Programming and Programming Language

"PROGRAMMING" is the word which we all come across quite often. Let us see what that means.

Learning Journal

Generate Random Number in Java

Look at the different methods to generate random numbers in Java.

Learning Journal

Dawn of Bigdata

Let us go through the birth of Bigdata.

Learning Journal