-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.java
More file actions
170 lines (149 loc) · 3.44 KB
/
Copy pathStringUtils.java
File metadata and controls
170 lines (149 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.xzd.tools;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
public class StringUtils {
/**
* 字符串转化为key-value形式
*
* @param str
* @return
* @throws IOException
*/
public static Properties str2Properties(String str) throws IOException {
Properties prop = new Properties();
InputStream is = null;
try {
is = new ByteArrayInputStream(str.getBytes());
prop.load(is);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return prop;
}
/**
* 字符串是否为空
*
* @param s
* @return
*/
public static boolean isEmpty(String s) {
return s == null || s.trim().isEmpty();
}
public static List<String> strToList(String str, String split) {
List<String> list = new ArrayList<String>();
if (!StringUtils.isEmpty(str)) {
// 去除括号
if (str.contains("(") && str.contains(")")) {
int start = str.indexOf("(");
int end = str.indexOf(")");
str = str.substring(start + 1, end);
}
String[] ss = str.split(split);
for (String s : ss) {
list.add(s);
}
}
return list;
}
public static String makeIpString(String[] ips, int count) {
StringBuilder builder = new StringBuilder();
builder.append("(");
for (int i = 0; i < count; i++) {
builder.append(ips[i]);
if (i == count - 1) {
builder.append(")");
} else {
builder.append(" ");
}
}
return builder.toString();
}
public static String join(String[] array) {
StringBuffer sb = new StringBuffer();
int length = array.length;
for (int i = 0; i < length; i++) {
sb.append(array[i]).append(",");
}
return sb.substring(0, sb.length() - 1).toString();
}
/**
* 将list的stirng元素转为以逗号相连接的字符串
*
* @author 26820
* @param list
* 元素如[1,2,3,4,5]
* @return 1,2,3,4,5,6
*/
public static String listToSTring(List<String> list) {
if (list == null || list.size() == 0) {
return "";
}
String result = "";
for (String str : list) {
if (!StringUtils.isEmpty(str.trim()))
result += (str + ",");
}
if (result.length() == 0) {
return "";
}
return result.substring(0, result.length() - 1);
}
/**
* 将strs2中的元素从strs1中移除
*
* @author 26820
* @param strs1
* 如1,2,3,4
* @param strs2
* 如2,4
* @return 返回1,3
*/
public static String removeString(String strs1, String strs2) {
String[] strArr1 = strs1.split(",");
String[] strArr2 = strs2.split(",");
List<String> arrayList1 = new ArrayList<String>(1);
List<String> arrayList2 = new ArrayList<String>(1);
Collections.addAll(arrayList1, strArr1);
Collections.addAll(arrayList2, strArr2);
for (String e : arrayList2) {
if (!StringUtils.isEmpty(e))
arrayList1.remove(e);
}
return listToSTring(arrayList1);
}
/**
* 字符数组转list
*
* @param strArray
* @return
*/
public static List<String> asList(String[] strArray) {
List<String> list = new ArrayList<String>();
if (strArray != null) {
for (String s : strArray)
list.add(s);
}
return list;
}
/**
* 如果字符串为空返回空字符串, 否则返回原字符串
*
* @param str
* @return
*/
public static String getEmptyStringIfIsEmpty(String str) {
if (isEmpty(str))
return "";
return str;
}
}