String Java


String Java: In every language strings and string manipulations are inevitable. knowing the importance of string operations, Designers of Java introduced two classes to manipulate strings- String and StringBuffer.

In C/C++, string is an array of characters with a terminating /0. But in Java, it is very different. String is given the status of a class and placed in java.lang package.

String Java class comes with many predefined methods that make string manipulations easier. One small example is given with few methods on String Java.
public class Demo
{
  public static void main(String args[])
  {                           
    String str1 = "www.yahoo.com";                // using endsWith() method
    if(str1.endsWith("com"))
      System.out.println("\nIt is a commercial Web site");

    int x = 10;                                   // to convert int to string form valueOf()
    String str2 = String.valueOf(x);
    System.out.println("int x in string form: " + str2);

    String str3 = "Lord Vishnu";                  // to extract part of string, substring()
    String str4 = str3.substring(5);
    System.out.println("Extracting part of Lord Vishnu: " + str4);

    String str5 = "radar";                        // to check for Palindrome
    if(str5.equals(new StringBuffer(str5).reverse().toString()))   // see how much simple, a single line of code
      System.out.println("Yes, radar is Palindrome");
  }
}

String JavaOutput Screenshot on String Java

Few methods are given in the above example of String Java class. Many more methods are given with Examples, string manipulations Techniques a Programmer can do, Palindrome example etc. are fully discussed in the following link of this same Web site: One stop destination for all Java String and StringBuffer operations.

Leave a Comment

Your email address will not be published.