String Class in Java

In this article, we are going to describe the generally useable class that is String class in java. Because mostly all the data is input as form of string so its have his own importance.

Introduction

 
In this article, we are going to describe the generally useable class, the String class in Java. Because most data is input as a string so it has its own importance.
 
string1.png 
 
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as an instance of this class.
 

String class in Java

 
A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word "abhishek " and the phrase "I am abhishek kumar dubey software engineer " are both strings. Even "12345" could be considered a string, if specified correctly. Typically, programmers must enclose strings in quotation marks for the data to be recognized as a string and not a number or variable name.
 
string2.gif 
 

Method Details (String operation)

  • char charAt(int index)- returns the character at the specified location.
  • int compareTo(String other)- returns a negative value if the string comes before another in dictionary order, a positive value if the string comes after another in dictionary order, or 0 if the strings are equal.
  • boolean endsWith(String suffix)- returns true if the string ends with the suffix.
  • boolean equals(Object other)- returns true if the string equals other.
  • boolean equalsIgnoreCase(String other)- returns true if the string equals another, except for upper/lowercase distinction.
  • int indexOf(String str)-
  • int indexOf(String str, int fromIndex)- return the start of the first substring equal to str, starting at index 0 or at fromIndex.
  • int lastIndexOf(String str)-
  • int lastIndexOf(String str, int fromIndex)- return the start of the last substring equal to str, starting at index 0 or at fromIndex.
  • int length()- returns the length of the string.
  • String replace(char oldChar, char newChar)- returns a new string that is obtained by replacing all characters oldChar in the string with newChar.
  • boolean startsWith(String prefix)-returns true if the string begins with the prefix.
  • String substring(int beginIndex)
  • String substring(int beginIndex, int endIndex)- return a new string consisting of all characters from beginIndex until the end of the string or until endIndex (exclusive).
  • String toLowerCase()- returns a new string containing all characters in the original string, with uppercase characters converted to lower case.
  • String toUpperCase()- returns a new string containing all characters in the original string, with lowercase characters converted to upper case.
  • String trim()- returns a new string by eliminating all leading and trailing spaces in the original string.
Example
  1. public class StringOperation {  
  2.     public static void main(String arg[]) {  
  3.         String s = " I am abhishek kumar dubey ";  
  4.         String s1 = new String("abhishek");  
  5.         System.out.println("the value charat(5)=" + s.charAt(5) + "\n");  
  6.         System.out.println("CompareTo method value=" + s.compareTo(s1) + "\n");  
  7.         System.out.println("is End with suffix =" + s.endsWith("dubey") + "\n");  
  8.         System.out.println("check equals=" + s.equals(s1) + "\n");  
  9.         System.out.println("check equals=" + s.equalsIgnoreCase(s1) + "\n");  
  10.         System.out.println("Index of dubey=" + s.indexOf("dubey") + "\n");  
  11.         System.out.println("Last Index of dubey=" + s.lastIndexOf("dubey")  
  12.             +  
  13.             "\n");  
  14.         System.out.println("The length of String s=" + s.length() + "\n");  
  15.         System.out.println("Replace 'a' by 'A'=" + s.replace('a''A') + "\n");  
  16.         System.out.println("check prefix=" + s.startsWith("I") + "\n");  
  17.         System.out.println("Sub String1=" + s.substring(10) + "\n");  
  18.         System.out.println("Sub String2=" + s.substring(1015) + "\n");  
  19.         System.out.println("change String in lower Case=" + s.toLowerCase()  
  20.             +  
  21.             "\n");  
  22.         System.out.println("change String in upper Case=" + s.toUpperCase()  
  23.             +  
  24.             "\n");  
  25.         System.out.println("Remove the space end and last=" + s.trim() + "\n");  
  26.     }  
  27. }  
Output
 
 Cmd output string.jpg