String Operations in Java

String Operations in JAVA

Java is known as a pure object oriented programming language and same as other languages it also contains a wide range of variables like int, char, bool, and string, etc. For Strings, you can perform different string operations.

In this post we discuss following topics:

  • How to create a String in Java and String variables in java?
  • How to combine or concatenate two strings in java?
  • How to find the length of the string in java?
  • How to compare two strings in java?
  • How to find the index of a character in a string?
  • How to convert String to char array?
  • How to replace a character in a string in java?
  • How to split a string in java?
  • How to convert a lowercase string to uppercase in Java?

String defining and declaration in java:

We can declare a string in java using two methods one is like simple variables declarations:

The thing to remember in Java the string keyword starts with capital S.

Concatenate string in Java:

To combine two string in java concat() function is used.

Find length of string:

To find the length of the string in Java, we use the length() function.

Compare two strings:

To compare two strings in java we use the compareTo() function.

Find index of a character in string:

The indexOf() function is used to find any index of character in string:

Convert String to char array:

To a convert a string to character array toCharArray() function is used:

Replace character in string:

To replace a character and a substring in a string with some other String replaceAll() function is used in Java.

Split a string in Java:

To split a string in Java string.split() function is used:

To lower Case:

To convert a string in lower case order toLowerCase() function is used.

To Uppercase in Java:

To convert a string to upper case toUpperCase() function is used.