com.tencent.polaris.api.utils.StringUtils Maven / Gradle / Ivy
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.
*/
package com.tencent.polaris.api.utils;
import java.util.Objects;
public class StringUtils {
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for (int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
}
return true;
}
public static String defaultString(String value) {
if (null == value) {
return "";
}
return value;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean equals(String str1, String str2) {
return Objects.equals(str1, str2);
}
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static String replace(String text, String searchString, String replacement) {
return replace(text, searchString, replacement, -1);
}
/**
* 替换字符串
*
* @param text text
* @param searchString searchString
* @param replacement replacement
* @param max max
* @return 结果
*/
public static String replace(String text, String searchString, String replacement, int max) {
if (!isEmpty(text) && !isEmpty(searchString) && replacement != null && max != 0) {
int start = 0;
int end = text.indexOf(searchString, start);
if (end == -1) {
return text;
} else {
int replLength = searchString.length();
int increase = replacement.length() - replLength;
increase = increase < 0 ? 0 : increase;
increase *= max < 0 ? 16 : (max > 64 ? 64 : max);
StringBuilder buf = new StringBuilder(text.length() + increase);
for (; end != -1; end = text.indexOf(searchString, start)) {
buf.append(text.substring(start, end)).append(replacement);
start = end + replLength;
--max;
if (max == 0) {
break;
}
}
buf.append(text.substring(start));
return buf.toString();
}
} else {
return text;
}
}
/**
* Test whether the given string matches the given substring
* at the given index.
*
* @param str the original string (or StringBuilder)
* @param index the index in the original string to start matching against
* @param substring the substring to match at the given index
*/
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
if (index + substring.length() > str.length()) {
return false;
}
for (int i = 0; i < substring.length(); i++) {
if (str.charAt(index + i) != substring.charAt(i)) {
return false;
}
}
return true;
}
}