forked from maxliaops/Java_Web_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.java
More file actions
150 lines (139 loc) · 4.17 KB
/
StringUtils.java
File metadata and controls
150 lines (139 loc) · 4.17 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
package com.core;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: MR</p>
* @version 1.0
*/
//对字符串进行GBK编码
public class StringUtils {
public static String toChinese(String strvalue) {
try {
if (strvalue == null) {
return "";
} else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
} catch (Exception e) {
return "";
}
}
//对输入的字符串进行一次编码转换,防止SQL注入
public static String StringtoSql(String str) {
str = nullToString(str, "");
try {
str = str.trim().replace('\'', (char) 1);
} catch (Exception e) {
return "";
}
return str;
}
//对字符串进行二次编码转换,防止出库时异常
public static String SqltoString(String str) {
str = nullToString(str, "");
try {
str = str.replace( (char) 1, '\'').trim();
} catch (Exception e) {
return "";
}
return str;
}
//对字符串进行Unicode编码
public static String toUnicode(String strvalue) {
try {
if (strvalue == null) {
return null;
} else {
strvalue = new String(strvalue.getBytes("GBK"), "ISO8859_1");
return strvalue;
}
} catch (Exception e) {
return "";
}
}
//判断是否为当前时间
public static boolean compareNowTime(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = format.parse(date); //返回一个Date类的实例
} catch (ParseException ex) {
}
if (System.currentTimeMillis() - 259200000 < d.getTime()) {
return true;
}
return false;
}
//判断用户输入的是否是数字或字母
public static boolean isID(String str) {
if (str != null && str.length() > 0) {
if (str.charAt(0) < 57 && str.charAt(0) > 48) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < 65 && str.charAt(i) > 57 || str.charAt(i) > 90
&& str.charAt(i) < 97 && str.charAt(i) != 95 || str.charAt(i) > 122 ||
str.charAt(i) < 48) {
return false;
}
}
return true;
}
return false;
}
//对输入数据中的HTML字符进行转换
public static final String escapeHTMLTags(String input) {
if (input == null || input.length() == 0) {
return input;
}
StringBuffer buf = new StringBuffer(input.length());
char ch = ' ';
for (int i = 0; i < input.length(); i++) {
ch = input.charAt(i);
if (ch == '<') {
buf.append("<");
} else if (ch == '>') {
buf.append(">");
} else {
buf.append(ch);
}
}
String str=buf.toString();
str=str.replace(" "," ");
str=str.replace("\r\n","<br>");
return str;
}
//处理字符串中的空值
public static final String nullToString(String v, String toV) {
if (v == null) {
v = toV;
}
return v;
}
// 对SQL语句中输入的空值进行处理
public static final String SqlToLink(String str) {
str = StringUtils.nullToString(str, "");
if ("".equals(str)) {
str = " LIKE '%' ";
} else {
str = (" LIKE '%" + str + "%' ");
}
return str;
}
//将整型值转换为字符串
public static final String SqlToLink(int i) {
String str = "";
try {
str = new Integer(i).toString();
} catch (Exception e) {}
if (i == -1) {
str = "";
}
return StringUtils.SqlToLink(str);
}
}