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)

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(10, 15) + "\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
Read more >> Java String Tutorial