Strings in Java

Most of the data that transmits on internet is in the form of groups of characters, such group of characters are called strings. A string represents a group of characters. In java, string is an object of string class and not a character array as inn case of c/c++ programming languages.

In java, string is a class in java.lang package, but in java all classes are considered as data types. So, we can take string as both a class and a data type.

String s = "Java";

Here, s is a variable of the data type ‘String’.

Creating Strings

There are three ways to create strings in java

  • We can create a string just by assigning a group of characters to a string type variable
String s; // declaring string type variable
s = "Java"; // assigning a group of characters to it
  • We can create an object to string class by allocating memory using new operator. This is just like creating an object to any class as given below
String s = new String("java");
  • We can also create by converting the character array into strings.
char arr[] = {'J','a','v','a'}; 
String s = new String(arr); // string object is created and array name is passed to it

Here, the string object s contains the string Java, which means all the characters of the array are copied into the string.

String Class Methods

  • concat(String s)
  • int length()
  • char charAt(int i)
  • int compareTo(String s)
  • int compareToIgnoreCase(String s)
  • boolean equals(String s)
  • boolean equalsIgnoreCase(String s)
  • boolean startsWith(String s)
  • boolean endsWith(String s)
  • int indexOf(String s)
  • string replace(char c1, char c2)
  • string substring(int i)
  • string toLowerCase()
  • string toUpperCase()
  • string trim()

concat(String s)

This method concatenates or joins two or more strings and return the resultant string as a result. Here the concat is the method name.


s1 = "Hello"
s2 = "Java"
String s3 = s1.concat(s2); // output will be "Hello Java"

int length()

This method returns the length or number of characters of a string. For example, s1.length() gives the number of characters in the String s1.

Example: s1.length() gives the number of characters in the string

char charAt(int i)

This method returns the character at the specified location i.

Example: Suppose we call this method as s1.charAt(5), then it gives the 5th character in the String s1.

int compareTo(String s)

This method is useful to compare two strings and to know which string is bigger or smaller.

Example: If we compare s1 and s2, if both are equal it will give 0 as output, if s1 is greater it will return positive number and if s2 is greater it will return negative number as output.

int compareToIgnoreCase(String s)

This is same as compareTo() method, but this does not take the case of string into consideration.

Example: for this method Java and java are one and the same as it ignores the case.

boolean equals(String s)

This method returns true if two strings are same, otherwise false, this is case-sensitive.

boolean equalsIgnoreCase(String s)

This is same as preceding method but, it performs case-insensitive commparison.

boolean startsWith(String s)

This method returns true if a string is beginning with the substring s. It is case-sensitive.

boolean endsWith(String s)

This method tests the ending of a string, if the string ends with sub string s, then it returns true. This method is also case-sensitive.

int indexOf(String s)

This method is called in the form, s1.indexOf(s2), and it returns an integer value.


s1 = "This is a book";
s2 = "is"
int n = s1.indexOf(s2); 
/* this will search for substring "is" in the main string "This is a book" and 
returns the output as `2` as "is" can be seen at the 2nd position. We can 
observe that "is" is present at 2nd and 5th positions but it will return only 
the first ocurring one.*/

string replace(char c1, char c2)

This method replaces all the occurrences of character c1 by a new character c2.

Example: s1 = "Hello" and we use replace method as s1.replace(’l’,‘x’), this will give output as “Hexxo”.

string substring(int i)

This method is useful to extract sub string from a main string. It returns a new string consisting of all characters from the position i until the end of the string.

Example: s1.substring(5) returns characters starting from 5th character till the end of s1.

string toLowerCase()

This method converts all the characters of the string into lower case, and returns that lower-cased string.

string toUpperCase()

This method converts all the characters of the string into upper case, and returns that upper-cased string.

string trim()

This method removes spaces from the beginning and ending of a string.

Example: if a string is written as " Hello Java “, the trim method will remove the unnecessary spaces before and after the string and the output will be “Hello Java”.

Subscribe For More Content