Java String Ends With endsWith(StringBuilder sb, String suffix)

Here you can find the source of endsWith(StringBuilder sb, String suffix)

Description

Tests if this StringBuilder ends with the specified suffix.

License

LGPL

Parameter

Parameter Description
suffix the suffix.

Return

true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to the specified StringBuilder object as determined by comparing the corresponding string obtained by calling the method.

Declaration

public static boolean endsWith(StringBuilder sb, String suffix) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//from   w  w  w  . jav a 2  s .  c o m
      * Tests if this <tt>StringBuilder</tt> ends with the specified suffix.
      *
      * @param suffix the suffix.
      * @return  <code>true</code> if the character sequence represented by the
      *          argument is a suffix of the character sequence represented by
      *          this object; <code>false</code> otherwise. Note that the
      *          result will be <code>true</code> if the argument is the
      *          empty string or is equal to the specified
      *          <code>StringBuilder</code> object as determined by 
      *          comparing the corresponding string obtained by
      *          calling the {@link StringBuilder#toString()} method.
      */
    public static boolean endsWith(StringBuilder sb, String suffix) {
        return startsWith(sb, suffix, sb.length() - suffix.length());
    }

    /**
      * Tests if this string starts with the specified prefix.
      *
      * @param prefix the prefix.
      * @return  <code>true</code> if the character sequence represented by the
      *          argument is a prefix of the character sequence represented by
      *          this string; <code>false</code> otherwise.
      *          Note also that <code>true</code> will be returned if the
      *          argument is an empty string or is equal to this
      *          <code>StringBuilder</code> object as determined by 
      *          comparing the corresponding string obtained by
      *          calling the {@link StringBuilder#toString()} method.
      */
    public static boolean startsWith(StringBuilder sb, String prefix) {
        return startsWith(sb, prefix, 0);
    }

    /**
      * Tests if the substring of the specified <tt>StringBuilder</tt> 
      * beginning at the specified index starts with the specified prefix.
      *
      * @param sb
      * @param prefix the prefix.
      * @param offset where to begin looking in this string.
      * @return  <code>true</code> if the character sequence represented by the
      *          argument is a prefix of the substring of this object starting
      *          at index <code>offset</code>; <code>false</code> otherwise.
      *          The result is <code>false</code> if <code>toffset</code> is
      *          negative or greater than the length of this
      *          <code>StringBuilder</code> object; otherwise the result 
      *          is the same as the result of the expression
      *          <pre>
      *          sb.substring(offset).startsWith(prefix)
      *          </pre>
      */
    public static boolean startsWith(StringBuilder sb, String prefix, int offset) {
        if (offset < 0 || sb.length() - offset < prefix.length())
            return false;

        int len = prefix.length();
        for (int i = 0; i < len; ++i) {
            if (sb.charAt(offset + i) != prefix.charAt(i))
                return false;
        }
        return true;
    }
}

Related

  1. endsWith(StringBuffer buf, String s)
  2. endsWith(StringBuffer buffer, String suffix)
  3. endsWith(StringBuffer in, String ending)
  4. endsWith(StringBuilder sb, String end)
  5. endsWith(StringBuilder sb, String s)
  6. endsWith3(String string, int char1, int char2, int char3)
  7. endsWithAny(final byte[] str, int startIndex, int endIndex, final byte... chars)
  8. endsWithAny(String str, String... args)
  9. endsWithAny(String string, String searchStrings[])