Java String Sub String substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)

Here you can find the source of substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)

Description

This calculates a substring from an array of StorableFields.

License

Apache License

Parameter

Parameter Description
start character offset to start
end character offset to end
fieldValues array of Strings to process
offsetGap offsetGap as typically returned by Analyzer's .getOffsetGap()
interFieldJoiner string to use to mark that a substring goes beyond a single field entry

Return

substring, potentially empty, never null.

Declaration

public static String substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap,
        String interFieldJoiner) 

Method Source Code

//package com.java2s;
/*//from ww w.j  a va 2  s.c om
 * 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 {
    /**
     * This calculates a substring from an array of StorableFields.
     * 
     * This attempts to do the best job possible, and at worst will
     * return an empty string.  If the start or end is within a gap,
     * or before 0 or after the total number of characters, this will
     * gracefully (blithely?) handle those cases.
     * 
     * 
     * @param start character offset to start
     * @param end character offset to end
     * @param fieldValues array of Strings to process
     * @param offsetGap offsetGap as typically returned by Analyzer's .getOffsetGap()
     * @param interFieldJoiner string to use to mark that a substring goes beyond a single
     * field entry
     * @return substring, potentially empty, never null.
     */
    public static String substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap,
            String interFieldJoiner) {
        start = (start < 0) ? 0 : start;
        end = (end < 0) ? 0 : end;

        if (start > end) {
            start = end;
        }

        int charBase = 0;
        StringBuilder sb = new StringBuilder();
        int lastFieldIndex = 0;
        int localStart = 0;
        boolean foundStart = false;
        //get start
        for (int fieldIndex = 0; fieldIndex < fieldValues.length; fieldIndex++) {
            String fString = fieldValues[fieldIndex];
            if (start < charBase + fString.length()) {
                localStart = start - charBase;
                lastFieldIndex = fieldIndex;
                foundStart = true;
                break;
            }
            charBase += fString.length() + offsetGap;
        }
        if (foundStart == false) {
            return "";
        }
        //if start occurred in a gap, reset localStart to 0
        if (localStart < 0) {
            sb.append(interFieldJoiner);
            localStart = 0;
        }
        //now append and look for end
        for (int fieldIndex = lastFieldIndex; fieldIndex < fieldValues.length; fieldIndex++) {
            String fString = fieldValues[fieldIndex];

            if (end <= charBase + fString.length()) {
                int localEnd = end - charBase;
                //must be in gap
                if (charBase > end) {
                    return sb.toString();
                }
                if (fieldIndex != lastFieldIndex) {
                    sb.append(interFieldJoiner);
                }
                sb.append(fString.substring(localStart, localEnd));
                break;
            } else {
                if (fieldIndex != lastFieldIndex) {
                    sb.append(interFieldJoiner);
                }
                sb.append(fString.substring(localStart));
                localStart = 0;
            }
            charBase += fString.length() + offsetGap;
        }
        return sb.toString();
    }
}

Related

  1. substringEL(String str, int index, String defaultValue)
  2. substringEnd(String string, int start, int length)
  3. substringEquals(final String s1, final int fromIndex1, final int toIndex1, final String s2, final int fromIndex2, final int toIndex2)
  4. subStringExe(String s, String jmp, String sb, String se)
  5. substringFromMatch(String match, String string)
  6. substringGuarded(String s, int position, int count)
  7. subStringIgnoreCase(String str, String separator, Integer stratNum, Integer endNum)
  8. substringInBetween(String name, String prefix, String delimiter)
  9. substringL(String s, int i, int len)