public class Strings extends Object
| 构造器和说明 |
|---|
Strings() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
cleanPath(String path)
文件路径规范化,如“path/..”内部的点号
注意:windows的文件分隔符“\”会替换为“/”
|
static boolean |
containsAny(String str,
List<String> searches) |
static int |
count(String text,
String str)
Count str occur on text.
|
static List<String> |
csvSplit(List<String> list,
String s,
int off,
int len)
Split a quoted comma separated string to a list
Handle rfc4180-like
CSV strings, with the exceptions:
quoted values may contain double quotes escaped with back-slash
Non-quoted values are trimmed of leading trailing white space
trailing commas are ignored
double commas result in a empty string value
|
static String[] |
csvSplit(String s)
Parse a CSV string using
csvSplit(List,String, int, int)
use in {@link cn.ponfee.commons.web.WebContext.WebContextFilter) |
static String[] |
csvSplit(String s,
int off,
int len)
Parse a CSV string using
csvSplit(List, String, int, int) |
static String |
escapeRegex(String text)
Escape the regex characters: $()*+.[]?
|
static String |
escapeSql(String str)
Escapes the characters in a
String to be suitable to pass to
an SQL query. |
static Map<String,String> |
fromArgs(String[] args)
Parse main method args, such as: [name1=value,name2=value2,...]
|
static String[] |
hexadecimal(String text)
Convert to hexadecimal string array
|
static String |
ifBlank(String str,
String defaultStr) |
static String |
ifEmpty(String str,
String defaultStr)
如果为空则设置默认
|
static boolean |
isMatch(String s,
String p)
'?'
|
static String |
join(Collection<?> coll) |
static String |
join(Collection<?> coll,
String delimiter) |
static <T> String |
join(Collection<T> coll,
String delimiter,
Function<T,String> mapper,
String open,
String close)
集合拼接为字符串
join(Arrays.asList("a","b","c"), ",", "(", ")") -> (a),(b),(c)
|
static String |
mask(String text,
int start,
int len) |
static String |
mask(String text,
int start,
int len,
char maskChar)
遮掩(如手机号中间4位加*)
|
static String |
mask(String text,
String regex,
String replacement) |
static String |
safePath(String path)
Returns a safe file system path that forbid access parent dir
|
static String[] |
slice(String str,
int segment)
字符串分片
slice("abcdefghijklmn", 5) -> ["abc","def","ghi","jkl","mn"]
|
static String |
toCamelcaseName(String separatedName,
char separator)
带分隔符名字转驼峰,如下划线转换为驼峰:CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, underscoreName);
1 LOWER_HYPHEN 连字符的变量命名规范如lower-hyphen
2 LOWER_UNDERSCORE c++变量命名规范如lower_underscore
3 LOWER_CAMEL java变量命名规范如lowerCamel
4 UPPER_CAMEL java和c++类的命名规范如UpperCamel
5 UPPER_UNDERSCORE java和c++常量的命名规范如UPPER_UNDERSCORE
|
static String |
toSeparatedName(String camelcaseName,
char separator)
驼峰转为带分隔符名字,如驼峰转换为下划线:CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, camelCaseName);
|
public static String join(Collection<?> coll)
public static String join(Collection<?> coll, String delimiter)
public static String[] hexadecimal(String text)
text - the textpublic static <T> String join(Collection<T> coll, String delimiter, Function<T,String> mapper, String open, String close)
join(Arrays.asList("a","b","c"), ",", "(", ")") -> (a),(b),(c)
coll - 集合对象delimiter - 分隔符mapper - 对象转Stringopen - 每个元素添加的前缀close - 每个元素添加的后缀IteratorUtils.toString(Iterator, org.apache.commons.collections4.Transformer, String, String, String),
IterableUtils.toString(Iterable, org.apache.commons.collections4.Transformer, String, String, String),
String.join(CharSequence, CharSequence...),
Collectors.joining(CharSequence, CharSequence, CharSequence),
StringUtils.join(List, String, int, int),
Joiner.join(Object, Object, Object...),
StringJoiner(CharSequence, CharSequence, CharSequence)public static Map<String,String> fromArgs(String[] args)
args - the argspublic static String mask(String text, int start, int len, char maskChar)
text - 需要处理的字符串start - 开始位置len - 要处理的字数长度maskChar - 替换的字符public static int count(String text, String str)
text - the textstr - the stringpublic static String[] slice(String str, int segment)
str - segment - public static boolean isMatch(String s, String p)
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
isMatch("aa","a") = false
isMatch("aa","aa") = true
isMatch("aaa","aa") = false
isMatch("aa", "*") = true
isMatch("aa", "a*") = true
isMatch("ab", "?*") = true
isMatch("aab", "c*a*b") = false
s - the textp - the wildcard patterntrue if the string match patternpublic static String safePath(String path)
path - the pathpublic static String cleanPath(String path)
path - 文件路径public static String toSeparatedName(String camelcaseName, char separator)
camelcaseName - the camelcase nameseparator - the separatorCaseFormat.to(CaseFormat, String)public static String toCamelcaseName(String separatedName, char separator)
separatedName - the separated nameseparator - the separatorCaseFormat.to(CaseFormat, String)public static String ifEmpty(String str, String defaultStr)
str - defaultStr - public static String escapeSql(String str)
Escapes the characters in aStringto be suitable to pass to an SQL query. For example, statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" + StringEscapeUtils.escapeSql("McHale's Navy") + "'"); At present, this method only turns single-quotes into doubled single-quotes ("McHale's Navy"=>"McHale''s Navy"). It does not handle the cases of percent (%) or underscore (_) for use in LIKE clauses. see http://www.jguru.com/faq/view.jsp?EID=8881
str - the string to escape, may be nullnull if null string inputpublic static String escapeRegex(String text)
text - the text stringpublic static String[] csvSplit(String s)
csvSplit(List,String, int, int)
use in {@link cn.ponfee.commons.web.WebContext.WebContextFilter)s - The string to parsepublic static String[] csvSplit(String s, int off, int len)
csvSplit(List, String, int, int)s - The string to parseoff - The offset into the string to start parsinglen - The len in characters to parsepublic static List<String> csvSplit(List<String> list, String s, int off, int len)
Handle rfc4180-like CSV strings, with the exceptions:
list - The Collection to split to (or null to get a new list)s - The string to parseoff - The offset into the string to start parsinglen - The len in characters to parseCopyright © 2023. All rights reserved.