Java String Ends With endsWith(StringBuffer buffer, String suffix)

Here you can find the source of endsWith(StringBuffer buffer, String suffix)

Description

Checks that a string buffer ends up with a given string.

License

Apache License

Parameter

Parameter Description
buffer the buffer to perform the check on
suffix the suffix

Return

true if the character sequence represented by the argument is a suffix of the character sequence represented by the StringBuffer object; false otherwise. Note that the result will be true if the argument is the empty string.

Declaration

public static boolean endsWith(StringBuffer buffer, String suffix) 

Method Source Code

//package com.java2s;
/*/*from  ww w  .ja  va2  s. com*/
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

public class Main {
    /**
     * Checks that a string buffer ends up with a given string. It may sound
     * trivial with the existing
     * JDK API but the various implementation among JDKs can make those
     * methods extremely resource intensive
     * and perform poorly due to massive memory allocation and copying. See
     * @param buffer the buffer to perform the check on
     * @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
     *          the StringBuffer object; <code>false</code> otherwise. Note that the
     *          result will be <code>true</code> if the argument is the
     *          empty string.
     */
    public static boolean endsWith(StringBuffer buffer, String suffix) {
        if (suffix.length() > buffer.length()) {
            return false;
        }
        // this loop is done on purpose to avoid memory allocation performance
        // problems on various JDKs
        // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
        // implementation is ok though does allocation/copying
        // StringBuffer.toString().endsWith() does massive memory
        // allocation/copying on JDK 1.5
        // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
        int endIndex = suffix.length() - 1;
        int bufferIndex = buffer.length() - 1;
        while (endIndex >= 0) {
            if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
                return false;
            }
            bufferIndex--;
            endIndex--;
        }
        return true;
    }
}

Related

  1. EndsWith(String x, String z)
  2. endsWith(String[] endsWith, String line)
  3. endsWith(String[] searchStrings, String text)
  4. endsWith(String[] searchStrings, String text)
  5. endsWith(StringBuffer buf, String s)
  6. endsWith(StringBuffer in, String ending)
  7. endsWith(StringBuilder sb, String end)
  8. endsWith(StringBuilder sb, String s)
  9. endsWith(StringBuilder sb, String suffix)