Skip to content

Commit 92f1664

Browse files
author
shy-coder
committed
day16:StringUtil
1 parent 192e4f0 commit 92f1664

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.shy.day16;
2+
3+
import java.text.DecimalFormat;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
/**
9+
* @ClassName StringUtil
10+
* @Author shy
11+
* @Date 2020/11/4
12+
**/
13+
public class StringUtil {
14+
/**
15+
* 判断是否是空字符串,null和""都返回 true
16+
* @param str
17+
* @return
18+
*/
19+
public static boolean isEmpty(String str){
20+
return str == null || "".equals(str);
21+
}
22+
23+
/**
24+
* 把String array of list 用给定的符号symbol连接成一个字符串
25+
* @param list 需要处理的列表
26+
* @param symbol 链接的符号
27+
* @return 处理后的字符串
28+
*/
29+
public static String joinString(List<Object> list, String symbol) {
30+
StringBuilder result = new StringBuilder();
31+
if (list != null){
32+
for (Object o : list){
33+
String temp = o.toString();
34+
if (temp.trim().length() > 0){
35+
result.append(temp).append(symbol);
36+
}
37+
}
38+
if (result.length() > 1) {
39+
result = new StringBuilder(result.substring(0, result.length()-1));
40+
}
41+
}
42+
return result.toString();
43+
}
44+
45+
/**
46+
* 字符串省略截取
47+
* 字符串截取到指定长度size+...的形式
48+
* @param subject 需要处理的字符串
49+
* @param size 截取的长度
50+
* @return 处理后的字符串
51+
*/
52+
public static String sunStringOmit(String subject, int size) {
53+
if (subject != null && subject.length() > size){
54+
subject = subject.substring(0,size) + "...";
55+
}
56+
return subject;
57+
}
58+
59+
/**
60+
* 隐藏邮件地址前缀
61+
* @param email Email邮件地址,列入ssss@qq.com
62+
* @return 返回已经隐藏前缀邮箱地址,如******@qq.com
63+
*/
64+
public static String getHideEmailPrefix(String email) {
65+
if (null != email) {
66+
int index = email.lastIndexOf("@");
67+
if (index > 0){
68+
email = repeat("*",index).concat(email.substring(index));
69+
}
70+
}
71+
return email;
72+
}
73+
74+
/**
75+
* repeat 通过源字符串重复生成N次新的字符串
76+
* @param src 源字符串 列如:空格(“ ”),星号(“*”),“浙江”
77+
* @param num 重复生成次数
78+
* @return 返回已生成的重复字符串
79+
*/
80+
private static String repeat(String src, int num) {
81+
StringBuilder s = new StringBuilder();
82+
for (int i = 0; i <num ; i++) {
83+
s.append(src);
84+
}
85+
return s.toString();
86+
}
87+
88+
/**
89+
* 截取字符串左侧的Num位截取到末尾
90+
* @param str1 处理的字符串
91+
* @param num 开始位置
92+
* @return 截取后的字符串
93+
*/
94+
public static String lTrim(String str1,int num){
95+
String tt = "";
96+
if (str1.length() >= num && !isEmpty(str1)) {
97+
tt= str1.substring(num, str1.length());
98+
}
99+
return tt;
100+
}
101+
102+
/**
103+
* 根据指定的字符串把源字符串分割成一个list
104+
* @param src 处理的字符串
105+
* @param patten 分割字符串
106+
* @return 处理后的list
107+
*/
108+
public static List<String> parseString2List(String src,String patten){
109+
List<String> list=new ArrayList<>();
110+
if (src != null) {
111+
String[] tt=src.split(patten);
112+
list.addAll(Arrays.asList(tt));
113+
}
114+
return list;
115+
}
116+
117+
/**
118+
* 格式化一个float
119+
* @param format 需要格式化的格式 such as #.00,#.#
120+
* @return 格式化后的字符串
121+
*/
122+
public static String formatDouble(double f,String format){
123+
DecimalFormat decimalFormat = new DecimalFormat(format);
124+
return decimalFormat.format(f);
125+
}
126+
127+
/**
128+
* 截取字符串左侧指定长度的字符串
129+
* @param input 输入字符串
130+
* @param count 截取长度
131+
* @return 截取的字符串
132+
*/
133+
public static String left(String input,int count){
134+
if (isEmpty(input)){
135+
return "";
136+
}
137+
count = Math.min(count, input.length());
138+
return input.substring(0,count);
139+
}
140+
141+
/**
142+
* 获取字符串str在String中出现的次数
143+
* @param str 子字符串
144+
*/
145+
public static String trimPunct(String str){
146+
if (isEmpty(str))
147+
return "";
148+
return str.replaceAll("[\\pP\\p{Punct}]","");
149+
}
150+
151+
/**
152+
* 获取字符串str在string中出现的次数
153+
* @param string 处理的字符串
154+
* @param str 子字符串
155+
*/
156+
public static int countSubStr(String string,String str){
157+
if ((str==null)||(str.length()==0||(string==null)||(string.length()==0)))
158+
return 0;
159+
int count=0;
160+
int index=0;
161+
while ((index=string.indexOf(str,index))!=-1){
162+
count++;
163+
index+= str.length();
164+
}
165+
return count;
166+
}
167+
168+
}

src/main/java/com/shy/day16/test.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.shy.day16;
2+
3+
/**
4+
* @ClassName test
5+
* @Author shy
6+
* @Date 2020/11/4
7+
**/
8+
public class test {
9+
public static void main(String[] args) {
10+
String email = StringUtil.getHideEmailPrefix("2437309168@qq.com");
11+
System.out.println("隐藏后的邮箱:" + email);
12+
int count = StringUtil.countSubStr("qwerabcdab", "ab");
13+
System.out.println("出现的次数:" + count);
14+
String str = StringUtil.left("HelloWorld", 3);
15+
System.out.println("左侧的字符串:" + str);
16+
}
17+
}

0 commit comments

Comments
 (0)