Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

7.31.2014

String: startsWith() - Tests if this string starts with the specified prefix

If you want to get whether a string starts with a specified string, you can use the java.lang.String class's two overloaded startsWith() method. It returns true if the string contains the prefix or false otherwise.

In single argument startsWith() method, the only argument is the string that is the prefix in the specified string.

In two argument startsWith() method, the first argument is the prefix and the second one is the position or offset, from where you would like to check.

Some points about the startsWith() method is given below:

  • You can use this method with string literal. Example: "Java".startsWith("J").
  • If you pass null as the first argument to this method, it will always return true.


Method Declaration:
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)

Live Example:
public class Demo {
    public static void main(String[] args) {
        String url = "Learn Java API";
        System.out.println("url: " + url);
        System.out.println("url.startsWith(\"Learn\"): " +url.startsWith("Learn"));
        System.out.println("url.startsWith(\"L\"): " + url.startsWith("L"));
        System.out.println("url.startsWith(\"\"): " + url.startsWith(""));
        System.out.println("url.startsWith(\"Java\"): " + url.startsWith("Java"));
        System.out.println("url.startsWith(\"Learn Java API Now\"): " + url.startsWith("Learn Java API Now"));
        
        System.out.println("url.startsWith(\"Learn\", 0): " + url.startsWith("Learn", 0));
        System.out.println("url.startsWith(\"Learn\", 1): " + url.startsWith("Learn", 1));
        System.out.println("url.startsWith(\"Java\", 6): " + url.startsWith("Java", 6));
        System.out.println("url.startsWith(\"Java\", 5): " + url.startsWith("Java", 5));
    }
}

Output:
url: Learn Java API
url.startsWith("Learn"): true
url.startsWith("L"): true
url.startsWith(""): true
url.startsWith("Java"): false
url.startsWith("Learn Java API Now"): false
url.startsWith("Learn", 0): true
url.startsWith("Learn", 1): false
url.startsWith("Java", 6): true
url.startsWith("Java", 5): false

Use the social sharing button below to spread the knowledge among others.

7.25.2014

String: contains() - Checks whether a CharSequence is present in a String

If you want to check whether a string contains a specified character sequence that implements CharSequence interface (String, StringBuffer, StringBuilder), you can use contains() method defined in java.lang.String class. This method takes a character sequence as single argument and return true if the specified argument is present in the string or false if it doesn’t.

Some critical points to be noted:
  • A CharSequence must be enclosed in double quotes (“a” and “abc” both are CharSequence).
  • If you pass empty string (“”), the method will always return true.

Exception:
If the passed argument is null, NullPointerException will be thrown.

Method Declaration:
public boolean contains(CharSequence s)

Live Example:


public class Demo {
    public static void main(String[] args) {
          String blogTitle = "Learn Java API";
          System.out.println("blogTitle: " + blogTitle);
          System.out.println("blogTitle.contains(\"Java\"): " + blogTitle.contains("Java"));
          System.out.println("blogTitle.contains(\"java\"): " + blogTitle.contains("java"));
          System.out.println("blogTitle.contains(\"j\"): " + blogTitle.contains("j"));
          System.out.println("blogTitle.contains(\"J\"): " + blogTitle.contains("J"));
          System.out.println("blogTitle.contains(\"\"): " + blogTitle.contains(""));
          System.out.println("blogTitle.contains(\"\\n\"): " + blogTitle.contains("\n"));
          try {
              System.out.println("blogTitle.contains(null): " + blogTitle.contains(null));
          } catch (NullPointerException nullPtrExp) {
              System.out.println("blogTitle.contains(null): " + "You can't pass null!!!");
          }       
    }  
}
Output: 
blogTitle: Learn Java API
blogTitle.contains("Java"): true
blogTitle.contains("java"): false
blogTitle.contains("j"): false
blogTitle.contains("J"): true
blogTitle.contains(""): true
blogTitle.contains("\n"): false
blogTitle.contains(null): You can't pass null!!!



Use the social sharing button below to spread the knowledge among others.