PostgreSqlUtil.java

package com.mycim.utils;

/**
 * pgSQL工具类 放置pgSQL内函数的代替操作
 *
 * @author zhi.cai
 * @version 6.0.0
 * @date 2020/9/2
 **/
public class PostgreSqlUtil {

    /**
     * 定位目标字符串在字符串中出现的位置 等同于 String.indexOf(s)
     *
     * @param str       字符串
     * @param searchStr 要查找的字符串
     * @return
     */
    public static int instr(String str, String searchStr) {
        return instr(str, searchStr, 1, 1);
    }

    /**
     * 定位目标字符串在字符串中出现的位置 注意: Postgre数据库中下标从1开始,而Java从0开始
     * <p>
     * instr('123.456.789.000', '.', 0)  --> 3 instr('123.456.789.000', '.', -1)  --> 11
     *
     * @param str        字符串
     * @param searchStr  要查找的字符串
     * @param beginIndex 开始查找的下标(下标负数,代表从后往前)
     * @return
     */
    public static int instr(String str, String searchStr, int beginIndex) {
        return instr(str, searchStr, beginIndex, 1);
    }

    /**
     * 定位目标字符串在字符串中出现的位置 注意: Postgre数据库中下标从1开始,而Java从0开始
     * <p>
     * instr('123.456.789.000', '.', 0, 1)   --> 3 instr('123.456.789.000', '.', -1, 1)  --> 11
     * instr('123.456.789.000', '.', 0, 2)   --> 7 instr('123.456.789.000', '.', -1, 2)  --> 7
     * instr('123.456.789.000', '1', 1, 1)  --> -1
     *
     * @param str        字符串
     * @param searchStr  要查找的字符串
     * @param beginIndex 开始查找的下标(下标负数,代表从后往前)
     * @param occurIndex 出现的次数
     * @return
     */
    public static int instr(String str, String searchStr, int beginIndex, int occurIndex) {
        String tempStr;
        int beg = 0;
        int position = -1;
        int occur_number = 0;
        int strLength = str.length();
        int searchStrLength = searchStr.length();

        if (beginIndex >= 0) {
            beg = beginIndex;
            while (beg < strLength - searchStrLength) {
                tempStr = str.substring(beg, beg + searchStrLength);
                position = tempStr.indexOf(searchStr);
                if (position > -1) {
                    occur_number += 1;
                    if (occur_number == occurIndex) {
                        return beg;
                    }
                }
                beg += 1;
            }
            return -1;
        } else {
            beg = strLength + beginIndex - searchStrLength + 1;
            while (beg > 0) {
                tempStr = str.substring(beg, beg + searchStrLength);
                position = tempStr.indexOf(searchStr);
                if (position > -1) {
                    occur_number += 1;
                    if (occur_number == occurIndex) {
                        return beg;
                    }
                }
                beg -= 1;
            }
            return -1;
        }
    }

    /**
     * 截取 startIndex 后的字符串, 长度越界返回Null substr("HelloWorld", 2)     --> lloWorld substr("HelloWorld", 999)
     * --> Null substr("HelloWorld", -5)    --> World //Oracle中 参数为负数代表从后往回截取5个字符
     * <p>
     * 注意,数据库中下标从1开始,传入0或1都是从1开始, Java从0开始
     *
     * @param str        需要截取的字符串
     * @param startIndex 第startIndex个字符开始截取后面所有的字符串
     * @return
     */
    public static String substr(String str, int startIndex) {
        if (str == null || str.length() < Math.abs(startIndex)) {
            return null;
        }

        if (startIndex < 0) {
            return str.substring(str.length() + startIndex);
        } else {
            return str.substring(startIndex);
        }

    }

    /**
     * 截取 startIndex 后长度为length的字符串 substr("HelloWorld", 2, 3)      --> llo substr("HelloWorld", 2, 999)
     * --> lloWorld substr("HelloWorld", 999, 1)    --> Null substr("HelloWorld", -5, 3)     --> Wor
     * substr("HelloWorld", -5, 99)    --> World substr("HelloWorld", -99, 1)    --> Null
     * <p>
     * 注意,数据库中下标从1开始,传入0或1都是从1开始, Java从0开始
     *
     * @param str        需要截取的字符串
     * @param startIndex 第startIndex个字符开始截取后面所有的字符串
     * @param length     要截取的字符串的长度
     * @return
     */
    public static String substr(String str, int startIndex, int length) {
        if (str == null || str.length() < Math.abs(startIndex)) {
            return null;
        }

        if (startIndex < 0) {
            length = length > str.length() + startIndex ? str.length() + startIndex : length;
            return str.substring(str.length() + startIndex, str.length() + startIndex + length);
        } else {
            length = length > str.length() - startIndex ? str.length() - startIndex : length;
            return str.substring(startIndex, startIndex + length);
        }
    }

    /**
     * Copy From Oracle 从字符串中移除目标字符串,且将目标字符串后的一位也移除 remove_substring("HelloWorld", "llo") --> "Heorld"
     * remove_substring("HelloWorld", "abc") --> "HelloWorld"
     *
     * @param str
     * @param subStr
     * @return
     */
    public static String remove_substring(String str, String subStr) {
        String resultStr;
        Integer startIdx;
        Integer endIdx;
        String strTemp;
        String strTemp2;

        resultStr = str;
        startIdx = str.indexOf(subStr);
        endIdx = startIdx + subStr.length();
        if (startIdx > 0) {
            strTemp = substr(str, 0, startIdx);
            if (endIdx == str.length()) {
                strTemp2 = "";
            } else {
                strTemp2 = substr(str, endIdx + 1);
            }
            resultStr = strTemp + strTemp2;
        }
        return resultStr;
    }

}