public class StringUtil extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
formatBinarySize(long size)
格式化字节大小,将字节大小转为
kb,mb,gb等格式 |
static int |
indexOf(String[] array,
String v)
查找字符串在数组中第一次出现的位置,查找是从数组头向尾逐一比较,时间复杂度
n(n为数组长度),
建议只应用于小数组查找,待查找字符串v可以为null,但数组不能为null |
static int |
indexOf(String str,
int ch,
int fromIndex,
int toIndex)
查找某个字符
ch在字符串str的位置,与String.indexOf(int, int)不同之处在于
后者从开始位置查找到字符串结尾,而前者需要指定一个结束位置查找范围在fromIndex到toIndex之间 |
static boolean |
isBlank(CharSequence cs)
检查字符串是否为
null或空白字符 |
static boolean |
isEmpty(String s)
检查字符串是否为空字符串,当字符串
s为null或String.isEmpty则返回true |
static boolean |
isNotBlank(CharSequence cs)
检查字符串不为
null或非空白字符 |
static boolean |
isNotEmpty(String s)
检查字符串不为空字符串,当字符串
s不为null且长度大小0则返回true |
static String |
lowFirstKey(String key)
首字母小写,转化是强制的它并不会检查空串,外部最好不要使用
|
static void |
swap(String[] values,
int a,
int b)
交换数组中的值,交换是强制的它并不会检查下标的范围,外部最好不要使用
|
static String |
timeToString(long t)
毫秒时间转字符串,通常用于格式化某段代码的耗时, 如:1h:3s or 4m:1s
|
static String |
toCamelCase(String name)
将字符串转为驼峰风格,仅支持将下划线
'_'风格转驼峰风格,内部不检查参数是否为null请谨慎使用
转换前 | 转换后
------------+------------
GOODS_NAME | goodsName
NAME | name
goods__name | goodsName
_goods__name| _goodsName
|
static String |
uppFirstKey(String key)
首字母大写,转化是强制的它并不会检查空串以及第二个字符是否为大字,外部最好不要使用
注意:本方法只适用于范围为
[97, 122]的ASCII值 |
public static boolean isEmpty(String s)
s为null或String.isEmpty则返回trues - 待检查字符串true当字符串为null或String.isEmptypublic static boolean isNotEmpty(String s)
s不为null且长度大小0则返回trues - 待检查字符串true当字符串不为null且长度大小0public static int indexOf(String[] array, String v)
n(n为数组长度),
建议只应用于小数组查找,待查找字符串v可以为null,但数组不能为nullarray - 查找源,不为nullv - 待查找字符串-1public static String uppFirstKey(String key)
注意:本方法只适用于范围为[97, 122]的ASCII值
key - 待处理字符串public static String lowFirstKey(String key)
key - 待处理字符串public static String toCamelCase(String name)
'_'风格转驼峰风格,内部不检查参数是否为null请谨慎使用
转换前 | 转换后 ------------+------------ GOODS_NAME | goodsName NAME | name goods__name | goodsName _goods__name| _goodsName
name - 待转换字符串public static void swap(String[] values, int a, int b)
values - 数组a - 指定交换下标b - 指定交换下标public static boolean isBlank(CharSequence cs)
null或空白字符cs - 待检查的字符串true 字符串为null或空白字符public static boolean isNotBlank(CharSequence cs)
null或非空白字符cs - 待检查的字符串true 字符串不为null或非空白字符public static String formatBinarySize(long size)
kb,mb,gb等格式size - 字节大小public static String timeToString(long t)
t - 毫秒时间public static int indexOf(String str, int ch, int fromIndex, int toIndex)
ch在字符串str的位置,与String.indexOf(int, int)不同之处在于
后者从开始位置查找到字符串结尾,而前者需要指定一个结束位置查找范围在fromIndex到toIndex之间str - 字符串源ch - 待查找的字符fromIndex - 起始位置(包含)toIndex - 结束位置(不包含)ch 在字符串的位置,未找到时返回-1Copyright © 2024. All rights reserved.