Determine the prefix

2021-06-05

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

Today, I was looking at the String class in the API specification. I found a method that says it determines the prefix. It is a method called String#startsWith().

There are many situations where you can look up a prefix. The first one that comes to mind is when the target is a code value. For example, there is a system where the first character of a code value represents a certain category.

In such a case, you can use the charAt() method to extract the first character and then determine the character. Then, what to do with the first two characters? One way is to combine the for statement with the charAt() method, but a simpler way is to use the substring() method. It would be common to use this method to extract the first two characters.

If the number of characters to be taken out is fixed, you can use the substring() method to take out the string first, and then do the judgment process. However, if the prefixes to be compared are of different lengths, this method may be difficult to use.

I think the startsWith() method is useful when the length of the prefix to be examined is not constant. startsWith() method has the following two methods.

  • public boolean startsWith(String prefix)
  • public boolean startsWith(String prefix, int toffset)

I often saw the first one. I will deal with the other one at the end of this article.

startsWith(String prefix)

I created a sample using a method with one argument. By the way, startsWith(String prefix) only returns startsWith(prefix, 0) internally.

The flow is described below.

  1. Initialize the String type array prefixes with the following values.
    element number contents
    0 “aero"
    1 “bio"
  2. Initialize the String type array words with the following values.
    element number contents
    0 “aerobics"
    1 “biography"
    2 “cardiovascular"
  3. The following process is performed for each element word of the array words.
    1. Do the following for each element prefix of the array prefixes.
      1. If the prefix of the variable word is equal to the variable prefix, output the following.
        “word: " + word + " prefix: " + prefix

The following are the source and execution results.

Source
public class HelloStartWith {

	public static void main(String[] args) {
        String[] prefixs = {"aero", "bio"};
        String[] words = {"aerobics", "biography", "cardiovascular"};

        for (String word : words) {
            for (String prefix : prefixs) {
                if (word.startsWith(prefix)) {
                    System.out.println("word: " + word + " prefix: " + prefix);
                }
            }
        }
	}

}
Execution results
word: aerobics prefix: aero
word: biography prefix: bio

This is a sample of scanning the whole word in the first loop, and then manipulating the whole prefix in the loop within it to determine the prefix. If the length of the prefix is not constant, the programming would be structured like this.

startsWith(String prefix, int toffset)

By the way, startsWith(String prefix, int toffset) does not seem to limit the range of toffset. It seems to return false even if you specify a value larger than the length of the string to be checked. Here is an experiment I did.

Source
public class HelloStartWithOffset {

	public static void main(String[] args) {
        String str = "ahoge";
        String prefix = "hoge";

        System.out.println(str.startsWith(prefix, 0));
        System.out.println(str.startsWith(prefix, 1));
        System.out.println(str.startsWith(prefix, 2));
        System.out.println(str.startsWith(prefix, 100));

    }

}
Execution results
false
true
false
false

The last one, startsWith, has a second argument larger than the length of str, but it returns false without error.

If there is a method to determine the prefix, what about the suffix, there is also an endsWith() method.

That’s all. I hope this is helpful to you.

Articles next and previous in the same category