[代码] java代码
001 | /** |
002 | * 分割字符串 |
003 | * |
004 | * @param str String 原始字符串 |
005 | * @param splitsign String 分隔符 |
006 | * @return String[] 分割后的字符串数组 |
007 | */ |
008 | @SuppressWarnings ( "unchecked" ) |
009 | public static String[] split(String str, String splitsign) { |
010 | int index; |
011 | if (str == null || splitsign == null ) |
012 | return null ; |
013 | ArrayList al = new ArrayList(); |
014 | while ((index = str.indexOf(splitsign)) != - 1 ) { |
015 | al.add(str.substring( 0 , index)); |
016 | str = str.substring(index + splitsign.length()); |
017 | } |
018 | al.add(str); |
019 | return (String[]) al.toArray( new String[ 0 ]); |
020 | } |
021 | |
022 | /** |
023 | * 替换字符串 |
024 | * |
025 | * @param from String 原始字符串 |
026 | * @param to String 目标字符串 |
027 | * @param source String 母字符串 |
028 | * @return String 替换后的字符串 |
029 | */ |
030 | public static String replace(String from, String to, String source) { |
031 | if (source == null || from == null || to == null ) |
032 | return null ; |
033 | StringBuffer bf = new StringBuffer( "" ); |
034 | int index = - 1 ; |
035 | while ((index = source.indexOf(from)) != - 1 ) { |
036 | bf.append(source.substring( 0 , index) + to); |
037 | source = source.substring(index + from.length()); |
038 | index = source.indexOf(from); |
039 | } |
040 | bf.append(source); |
041 | return bf.toString(); |
042 | } |
043 | |
044 | /** |
045 | * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号) |
046 | * |
047 | * @param str String 原始字符串 |
048 | * @return String 替换后的字符串 |
049 | */ |
050 | public static String htmlencode(String str) { |
051 | if (str == null ) { |
052 | return null ; |
053 | } |
054 | |
055 | return replace( "/"" , """ , replace( "<" , "<" , str)); |
056 | } |
057 | |
058 | /** |
059 | * 替换字符串,将被编码的转换成原始码(替换成双引号和小于号) |
060 | * |
061 | * @param str String |
062 | * @return String |
063 | */ |
064 | public static String htmldecode(String str) { |
065 | if (str == null ) { |
066 | return null ; |
067 | } |
068 | |
069 | return replace( """ , "/"" , replace( "<" , "<" , str)); |
070 | } |
071 | |
072 | private static final String _BR = "<br/>" ; |
073 | |
074 | /** |
075 | * 在页面上直接显示文本内容,替换小于号,空格,回车,TAB |
076 | * |
077 | * @param str String 原始字符串 |
078 | * @return String 替换后的字符串 |
079 | */ |
080 | public static String htmlshow(String str) { |
081 | if (str == null ) { |
082 | return null ; |
083 | } |
084 | |
085 | str = replace( "<" , "<" , str); |
086 | str = replace( " " , " " , str); |
087 | str = replace( "/r/n" , _BR, str); |
088 | str = replace( "/n" , _BR, str); |
089 | str = replace( "/t" , " " , str); |
090 | return str; |
091 | } |
092 | |
093 | /** |
094 | * 返回指定字节长度的字符串 |
095 | * |
096 | * @param str String 字符串 |
097 | * @param length int 指定长度 |
098 | * @return String 返回的字符串 |
099 | */ |
100 | public static String toLength(String str, int length) { |
101 | if (str == null ) { |
102 | return null ; |
103 | } |
104 | if (length <= 0 ) { |
105 | return "" ; |
106 | } |
107 | try { |
108 | if (str.getBytes( "GBK" ).length <= length) { |
109 | return str; |
110 | } |
111 | } catch (Exception ex) { |
112 | } |
113 | StringBuffer buff = new StringBuffer(); |
114 | |
115 | int index = 0 ; |
116 | char c; |
117 | length -= 3 ; |
118 | while (length > 0 ) { |
119 | c = str.charAt(index); |
120 | if (c < 128 ) { |
121 | length--; |
122 | } else { |
123 | length--; |
124 | length--; |
125 | } |
126 | buff.append(c); |
127 | index++; |
128 | } |
129 | buff.append( "..." ); |
130 | return buff.toString(); |
131 | } |
132 | |
133 | /** |
134 | * 判断是否为整数 |
135 | * |
136 | * @param str 传入的字符串 |
137 | * @return 是整数返回true,否则返回false |
138 | */ |
139 | public static boolean isInteger(String str) { |
140 | Pattern pattern = Pattern.compile( "^[-//+]?[//d]*$" ); |
141 | return pattern.matcher(str).matches(); |
142 | } |
143 | |
144 | /** |
145 | * 判断是否为浮点数,包括double和float |
146 | * |
147 | * @param str 传入的字符串 |
148 | * @return 是浮点数返回true,否则返回false |
149 | */ |
150 | public static boolean isDouble(String str) { |
151 | Pattern pattern = Pattern.compile( "^[-//+]?[.//d]*$" ); |
152 | return pattern.matcher(str).matches(); |
153 | } |
154 | |
155 | /** |
156 | * 判断输入的字符串是否符合Email样式. |
157 | * |
158 | * @param str 传入的字符串 |
159 | * @return 是Email样式返回true,否则返回false |
160 | */ |
161 | public static boolean isEmail(String str) { |
162 | Pattern pattern = Pattern.compile( "^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$" ); |
163 | return pattern.matcher(str).matches(); |
164 | } |
165 | |
166 | /** |
167 | * 判断输入的字符串是否为纯汉字 |
168 | * |
169 | * @param str 传入的字符窜 |
170 | * @return 如果是纯汉字返回true,否则返回false |
171 | */ |
172 | public static boolean isChinese(String str) { |
173 | Pattern pattern = Pattern.compile( "[/u0391-/uFFE5]+$" ); |
174 | return pattern.matcher(str).matches(); |
175 | } |
176 | |
177 | /** |
178 | * 是否为空白,包括null和"" |
179 | * |
180 | * @param str |
181 | * @return |
182 | */ |
183 | public static boolean isBlank(String str) { |
184 | return str == null || str.trim().length() == 0 ; |
185 | } |
186 | |
187 | /** |
188 | * 判断是不是合法手机 |
189 | * handset 手机号码 |
190 | */ |
191 | public static boolean isHandset(String handset) { |
192 | try { |
193 | if (!handset.substring( 0 , 1 ).equals( "1" )) { |
194 | return false ; |
195 | } |
196 | if (handset== null || handset.length()!= 11 ) { |
197 | return false ; |
198 | } |
199 | String check = "^[0123456789]+$" ; |
200 | Pattern regex = Pattern.compile(check); |
201 | Matcher matcher = regex.matcher(handset); |
202 | boolean isMatched = matcher.matches(); |
203 | if (isMatched) { |
204 | return true ; |
205 | } else { |
206 | return false ; |
207 | } |
208 | } catch (RuntimeException e) { |
209 | return false ; |
210 | } |
211 | } |
212 | } |