StartsWith In Java String Class

StartsWith In Java String Class | Java provides a way to check whether a string starts with a specified character/string or not by using the starsWith() method. This is basically a string class method that returns a boolean value true or false.

Similar to startsWith() method String class also contains endsWith() method to check whether the string ends with the specified character/string or not. This method also returns a boolean value. In this post, we will cover the following methods:-

  1. public boolean startsWith(String prefix)
  2. public boolean startsWith(String prefix, int toffset)
  3. public boolean endsWith(String suffix)

Lets us see some example of these methods,

String name ="Java";
System.out.println(name.startsWith("J"));

Output:-

true

String name ="Java";
System.out.println(name.endsWith("a"));

Output:-

true

StartsWith In Java

The startswith() method has two variations. Lets us demonstrate both the variations:-

  1. public boolean startsWith(String prefix)
  2. public boolean startsWith(String prefix, int toffset)

1) startsWith(String prefix)
The startsWith in Java takes only one parameter prefix. If the string starts with the same substring or character then it returns true or else false. The startsWith() method is case sensitive carefully observe both the codes.

public class Main {
   public static void main(String args[]) {
      String web = "Know Program";
      System.out.println(web.startsWith("K"));
      System.out.println(web.startsWith("k"));
   }
}

Output:

true
false

Code which says false

2) startsWith(String prefix, int tofffset)
This startsWith in Java takes two parameters prefix and toffset, toffset is an integer value that takes an index of the string and searches the prefix at that particular index.

public class Main {
   public static void main(String args[]) {
      String web = "Know Program";
      System.out.println(web.startsWith("P", 5));
      System.out.println(web.startsWith("P", 1));
      System.out.println(web.startsWith("n", 1));
      System.out.println(web.startsWith("r", 9));
      System.out.println(web.startsWith("m", web.length()));
      System.out.println(web.startsWith("m", web.length()-1));
   }
}

Output:-

true
false
true
true
false
true

Java String startsWith Ignore Case

As we know startsWith() is case sensitive we try to ignore the case by using toUpperCase() and toLowerCase() methods which convert the specified string into the upper or lower case. After that, we can search for the string.

Example of startsWith in Java With Ignoring the Case

public class Main {
   public static void main(String args[]) {
      String name = "Know Program";
      System.out.println(name.toLowerCase().startsWith("k"));
      System.out.println(name.toUpperCase().startsWith("KNOW"));
      System.out.println(name.toLowerCase().startsWith("program"));
   }
}

Output:-

true
true
false

Java startswith Regex

Now we use regular expressions to check for the pattern which starts from the specified character. For this we need to import various classes of the java library that are locale, set, treeset, regex.Matcher, regex.Pattern. We find all the languages which start with ‘K’.

import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

   public static void main(String[] args) {

      Set<String> language = new TreeSet<>();
      Locale[] locales = Locale.getAvailableLocales();
      for (Locale locale : locales) {
         language.add(locale.getDisplayLanguage());
      }

      Pattern pattern = Pattern.compile("^K");
      System.out.println("Language name which have the pattern of " 
                         + pattern.pattern() + ": ");

      for (String lang : language) {
         Matcher matcher = pattern.matcher(lang);
         if (matcher.lookingAt()) {
            System.out.println(lang);
         }
      }
   }
}

Output:

Language name which have the pattern of ^K:
Kabuverdianu
Kabyle
Kako
Kalaallisut
Kalenjin
Kamba
Kannada
Kashmiri
Kazakh
Khmer
Kikuyu
Kinyarwanda
Konkani
Korean
Koyra Chiini
Koyraboro Senni
Kurdish
Kwasio
Kyrgyz

The endswith In Java

Now, we will check for the string which ends with a given character or substring by using the endsWith() method in java. Using endsWith() method we will be able to demonstrate how to check end of string in Java.

public class Main {
   public static void main(String args[]) {
      String name = "Know Program";
      System.out.println(name.endsWith("m"));
      System.out.println(name.endsWith("Program"));
      System.out.println(name.endsWith("program"));
      System.out.println(name.endsWith("now"));
   }
}

Output:

true
true
false
false

Java String endswith Regex

Now we use regular expressions to check for the pattern which ends with the specified character for this there need to import various classes in the java library that are locale, set, treeset, regex.Matcher, regex.Pattern. Here we search for a language that ends with “da”.

import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

   public static void main(String[] args) {

      Set<String> language = new TreeSet<>();
      Locale[] locales = Locale.getAvailableLocales();
      for (Locale locale : locales) {
         language.add(locale.getDisplayLanguage());
      }

      Pattern pattern = Pattern.compile(".*da$");
      System.out.println("Language name which have the pattern of " 
                         + pattern.pattern() + ": ");

      for (String lang : language) {
         Matcher matcher = pattern.matcher(lang);
         if (matcher.lookingAt()) {
            System.out.println(lang);
         }
      }
   }
}

Output:-

Language name which have the pattern of .*da$:
Ganda
Kannada
Kinyarwanda

Also see:- Java Secret Message Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.