Skip to main content

String Class in Java (Part 2)

>

The String Class: (Part 2)

Comparison Methods of String class:

  • Example: (equals)
    /*
    == cannot be used to check whether two strings has same content or not.
    == only checks whether the strings are objects of String class or not.
    To check the content equality, equals() method is used. */
    class test {
    public static void main(String ar[]) {
    String str1 = "Welcome to Java";
    String str2 = "Welcome to Java";
    String str3 = new String("Welcome to Java");
    System.out.println(str1==str2); // true. because str1 and str2 are of variables of same data type ->String
    System.out.println(str1==str3); // false. because, str1 is a varaible of type String and str3 is an object of class String.
    System.out.println(str1.equals(str2)); // true. because equals checks the content and the content of str1 and str2 is same.
    System.out.println(str1.equals(str3)); // true. content of str1 and str3 is the same.
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (equalsIgnoreCase)
    class test {
    public static void main(String ar[]) {
    String str1 = "Java PROGRAMMING";
    String str2 = "java programming";
    System.out.println(str1.equalsIgnoreCase(str2)); // true
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (compareTo)
    class test {
    public static void main(String ar[]) {
    String str1 = "A";
    String str2 = "a";
    System.out.println(str1.compareTo(str2)); // (65-97 = -32)(A-a)
    String str3 = "Demo";
    String str4 = "Eemo";
    System.out.println(str3.compareTo(str4)); // (68-69 = -1)(D-E)
    String str5 = "ABCF";
    String str6 = "ABAI";
    System.out.println(str5.compareTo(str6));
    /* check character 1: A-A=0
    check character 2: B-B=0
    check character 3: C-A=67-65 =2 (as soon as the score is greater then 0, no further check is done.
    And the last score is printed. So this prints 2) */
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (compareToIgnoreCase)
    class test {
    public static void main(String ar[]) {
    String str1 = "A";
    String str2 = "a";
    System.out.println(str1.compareToIgnoreCase(str2));
    //removes the case sensitivity and then checks. So this prints 0
    String str3 = "ABc";
    String str4 = "abC";
    System.out.println(str3.compareToIgnoreCase(str4));
    //removes the case sensitivity and then checks. So this prints 0
    String str5 = "b";
    String str6 = "A";
    System.out.println(str5.compareToIgnoreCase(str6));
    //removes the case sensitivity of str6 and then checks. Computes: b-a. prints 1
    String str7 = "A";
    String str8 = "b";
    System.out.println(str7.compareToIgnoreCase(str8));
    //removes the case sensitivity of str8 and then checks. Computes: A-B. prints -1
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (startsWith)
    class test {
    public static void main(String ar[]) {
    String str1 = "Hello!";
    String str2 = "h";
    System.out.println(str1.startsWith(str2)); //false. h is lowercase
    String str3 = "Hello!";
    String str4 = "H";
    System.out.println(str3.startsWith(str4)); // true
    String str5 = "Good Morning!";
    String str6 = "Good";
    System.out.println(str5.startsWith(str6)); // true
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (endsWith)
    class test {
    public static void main(String ar[]) {
    String str1 = "Hello!";
    String str2 = "o";
    System.out.println(str1.endsWith(str2));
    //false. 'o' is not the lastmost character
    String str3 = "Hello!";
    String str4 = "!";
    System.out.println(str3.endsWith(str4));
    // true. '!' is the lastmost character
    String str5 = "Good Morning!";
    String str6 = "G!";
    System.out.println(str5.endsWith(str6));
    // false. this string ends with g! and not with G!
    String str7 = "Good Morning!";
    String str8 = "g!";
    System.out.println(str7.endsWith(str8)); // true
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (contains)
    class test {
    public static void main(String ar[]) {
    String str1 = "Good Morning!";
    System.out.println(str1.contains("good")); //false. G is uppercase
    System.out.println(str1.contains("Good")); //true
    System.out.println(str1.contains("!")); //true
    System.out.println(str1.contains("nin")); //true
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
Click HERE for the Simple Methods of String class.
Click HERE for the Substring Methods of String class.

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...

Character Class in Java (Part 2)

> The Character Class: (Part 1) Methods of Character class: Example: Method -> isDigit() [validating name] Example: Method -> isLetter() [validating mobile number] Example: Method -> isLetterOrDigit() [validating user_id / username] Example: Method -> isLowerCase() Example: Method -> isUpperCase() Example: Method -> toUpperCase() and toLowerCase() Click HERE for the list of Methods of Character class. Click HERE for the ASCII Table for different Characters