Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2020 Sejin Im
*
* Licensed 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.
*/
package io.github.imsejin.common.util;
import io.github.imsejin.common.annotation.ExcludeFromGeneratedJacocoReport;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.NumberFormat;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String utilities
*/
public final class StringUtils {
/**
* Whitespace character.
*/
private static final char WHITE_SPACE = '\u0020';
@ExcludeFromGeneratedJacocoReport
private StringUtils() {
throw new UnsupportedOperationException(getClass().getName() + " is not allowed to instantiate");
}
/**
* Checks whether the string is null or empty.
*
*
*
* @param str string
* @return whether the string is null or empty
*/
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
/**
* If the string is null or empty, this returns default string.
*
* If not, this returns original string.
*
*
*
* @param str original string
* @param defaultValue default string
* @return original string or default string
*/
public static String ifNullOrEmpty(String str, String defaultValue) {
return isNullOrEmpty(str) ? defaultValue : str;
}
/**
* If the string is null or empty, this returns default string.
*
* If not, this returns original string.
*
*
*
* @param str string
* @return whether the string is null or blank
*/
public static boolean isNullOrBlank(String str) {
if (isNullOrEmpty(str)) return true;
for (char c : str.toCharArray()) {
if (!Character.isWhitespace(c)) return false;
}
return true;
}
/**
* If the string is null or blank, this returns default string.
*
* If not, this returns original string.
*
*
*
* @param str original string
* @param defaultValue default string
* @return original string or default string
*/
public static String ifNullOrBlank(String str, String defaultValue) {
return isNullOrBlank(str) ? defaultValue : str;
}
/**
* If the string is null or blank, this returns default string.
*
* If not, this returns original string.
*
*
*
* @param str string
* @return whether the string is numeric
*/
public static boolean isNumeric(String str) {
if (isNullOrEmpty(str)) return false;
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) return false;
}
return true;
}
/**
* Fills the start of string with whitespace.
*
*
*
* @param len targeted string length;
* If it is less than the length of origin string,
* this returns it as it is.
* @param origin string that needs to be padded
* @return padded string
* @see #repeat(String, int)
*/
public static String padStart(int len, @Nonnull String origin) {
return padStart(len, origin, String.valueOf(WHITE_SPACE));
}
/**
* Fills the start of string with another string.
*
*
*
* @param len targeted string length;
* If it is less than the length of origin string,
* this returns it as it is.
* @param origin string that needs to be padded
* @param appendix string to be appended to origin string
* @return padded string
* @see #repeat(String, int)
*/
public static String padStart(int len, @Nonnull String origin, String appendix) {
int originLen = origin.length();
if (originLen >= len) return origin;
return repeat(appendix, len - originLen) + origin;
}
/**
* Fills the end of string with whitespace.
*
*
*
* @param len targeted string length;
* If it is less than the length of origin string,
* this returns it as it is.
* @param origin string that needs to be padded
* @return padded string
* @see #repeat(String, int)
*/
public static String padEnd(int len, @Nonnull String origin) {
return padEnd(len, origin, String.valueOf(WHITE_SPACE));
}
/**
* Fills the end of string with another string.
*
*
*
* @param len targeted string length;
* If it is less than the length of origin string,
* this returns it as it is.
* @param origin string that needs to be padded
* @param appendix string to be appended to origin string
* @return padded string
* @see #repeat(String, int)
*/
public static String padEnd(int len, @Nonnull String origin, String appendix) {
int originLen = origin.length();
if (originLen >= len) return origin;
return origin + repeat(appendix, len - originLen);
}
/**
* Returns the number of strings to be found at origin string.
*
*
If you input empty string as keyword,
* this returns length of the origin string.
*
* @param origin origin string
* @param keyword string to be found
* @return count of inclusions
*/
public static int countOf(@Nonnull String origin, @Nonnull String keyword) {
// If don't, will go into infinite loop.
if (keyword.isEmpty()) return origin.length();
int keywordLen = keyword.length();
int count = 0;
for (int i = origin.indexOf(keyword); i >= 0; i = origin.indexOf(keyword, i + keywordLen)) {
count++;
}
return count;
}
/**
* Reverses all the characters in a string.
*
* @param str string to be reversed
* @return reversed string
* @see StringBuilder#reverse
*/
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
/**
* 가장 마지막에 일치하는 문구를 원하는 문구로 대체한다.
*
*
*
* @param amount amount number
* @return formatted number with comma
*/
public static String formatComma(String amount) {
return NumberFormat.getInstance(Locale.US).format(Long.parseLong(amount));
}
/**
* Replicates a string as many times as you want.
*
*
*
* @param str string to be repeated
* @param cnt repetition count
* @return repeated string
*/
public static String repeat(String str, int cnt) {
return String.join("", Collections.nCopies(cnt, str));
}
/**
* Replicates a character as many times as you want.
*
*
*
* @param src source string
* @param pattern pattern of regular expression
* @param group group number you want to get value of
* @return captured string
*/
@Nullable
public static String find(@Nonnull String src, @Nonnull Pattern pattern, int group) {
Matcher matcher = pattern.matcher(src);
String result = null;
while (matcher.find()) {
result = matcher.group(group);
}
return result;
}
/**
* Finds the captured string with regular expression.
*
*
{@code
* find("
A
", "<.+>.*<\/(.+)>", 1); // div
* }
*
* @param src source string
* @param regex regular expression
* @param group group number you want to get value of
* @return captured string
*/
@Nullable
public static String find(@Nonnull String src, @Nonnull String regex, int group) {
Matcher matcher = Pattern.compile(regex).matcher(src);
String result = null;
while (matcher.find()) {
result = matcher.group(group);
}
return result;
}
/**
* Finds the captured strings with regular expression.
*
*
*
* @param src source string
* @param pattern pattern of regular expression
* @param groups group numbers you want to get value of
* @return map whose key is group number and whose value is a captured string.
*/
public static Map find(@Nonnull String src, @Nonnull Pattern pattern, int... groups) {
Matcher matcher = pattern.matcher(src);
Map result = new HashMap<>();
while (matcher.find()) {
for (int group : groups) {
result.put(group, matcher.group(group));
}
}
return result;
}
/**
* Finds the captured strings with regular expression.
*
*