String Functions in Java

Introduction

 
A string is a sequence of characters. Strings are very important in Java. There are various methods of strings in Java. Some of them are as follows.
 

Creation of string

 
We can create a string in Java in the following way.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String abc = "one way of creation";  
  5.   String bcd = new String("another way of creation");  
  6.   System.out.println(abc);  
  7.   System.out.println(bcd);  
  8.  }  
  9. }  
Output
 
creation of string
 
 

Copying Values of Array to String

 
We can copy the values contained by an array to the specified string in the following manner.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   char ch[] = {'c','o','m''p','u','t','e','r'};  
  5.   String Str = String.copyValueOf(ch);  
  6.   System.out.println(Str);  
  7.  }  
  8. }  
Output
 
copying array values to string
 
 

Concatenation of Two Strings

 
There is a simple concat() method for the concatenation of strings in Java or we can also concat them in the following manner.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String a = "COMPUTER ";  
  5.   String b = "SCIENCE";  
  6.   String c = a + b;  
  7.   System.out.println("from first way    " + c);  
  8.   System.out.println("\n");  
  9.   c = a.concat(b);  
  10.   System.out.println("from second way   " + c);  
  11.  }  
  12. }  
Output
 
concatenation of strings
 
 

Comparing Two Strings

 
We can compare two strings using the equals() method as follows.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String a = "COMPUTER";  
  5.   String b = "COM";  
  6.   String c = "COMPUTER";  
  7.   if (a.equals(b)) {  
  8.    System.out.println("strings are equal");  
  9.   } else {  
  10.    System.out.println("strings are not equal");  
  11.   }  
  12.   if (a.equals(c)) {  
  13.    System.out.println("strings are equal");  
  14.   } else {  
  15.    System.out.println("strings are not equal");  
  16.   }  
  17.  }  
  18. }   
Output
 
comparing two strings
 
 

Getting Index of a Character or String From Other String

 
We can determine the index of a specific character or string from the other string using the indexOf() method in the following way. 
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String a = "COMPUTER";  
  5.   System.out.println("Index of character 'T' is " + a.indexOf('T'));  
  6.   System.out.println("Index of string 'PUT' is " + a.indexOf("PUT"));  
  7.  }  
  8. }   
Output
 
finding index

Replacing Characters in the String

 
We can easily replace a character in the string with another character using the replace() method as follows.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String a = "COMPUTER";  
  5.   System.out.println("Index of character 'T' is " + a.indexOf('T'));  
  6.   System.out.println("Index of string 'PUT' is " + a.indexOf("PUT"));  
  7.  }  
  8. }  
Output
 
replacing character from string
 
 

Changing the Case of Characters of the String

 
We can change the case of the characters of the string by using the toLowerCase() and toUpperCase() methods as follows.
  1. package demo99;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String abc = "COMPUTER";  
  5.   System.out.println("To Lower Case: " + abc.toLowerCase());  
  6.   System.out.println("To Upper Case: " + abc.toUpperCase());  
  7.  }  
  8. }   
Output
 
changing case of characters in string
 

Summary

 
This article explains the various string functions in Java.


Similar Articles