java字符工具类_【Java】字符串工具类

本文介绍了如何通过StringUtils类实现IP地址校验、空字符串判断、纯数字检测、字符串长度计算、字符串数组转List、填充字符串及数字BCD转换等实用功能,适合Java开发者日常编码中遇到的字符串处理需求。
摘要由CSDN通过智能技术生成

1 importandroid.annotation.SuppressLint;2

3 importjava.io.UnsupportedEncodingException;4 importjava.util.ArrayList;5 importjava.util.List;6 importjava.util.regex.Matcher;7 importjava.util.regex.Pattern;8

9 public classStringUtils {10

11 /**

12 * 判断ip合法性13 */

14 @SuppressLint("NewApi")15 public static booleanisMacthIp(String ip) {16 if (ip != null && !ip.isEmpty()) {17 //定义正则表达式

18 String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."

19 + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."

20 + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."

21 + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; //判断ip地址是否与正则表达式匹配

22 if(ip.matches(regex)) {23 return true;24 }25 }26 return false;27 }28

29 /**

30 * 判断字符串是否为空31 */

32 public static booleanisEmpty(String str){33 if (str==null || "".equals(str)){34 return true;35 }36 return false;37 }38

39 /**

40 * 判断字符串是否为空(包括对"null")41 */

42 public static booleanisNullOrEmpty(String str){43 if (str==null || "".equals(str) || "null".equals(str) ){44 return true;45 }46 return false;47 }48

49

50 /**

51 * 判断字符串是否纯数字52 */

53 public static booleanisDigital(String str){54 if (!isEmpty(str))55 return str.matches("[0-9]+");56 return false;57 }58

59 /**

60 * 计算含有中文的字符串长度61 *@paramvalue 字符串(支持含中文字符串)62 *@return

63 */

64 public static intlength(String value) {65 int valueLength = 0;66 String chinese = "[\u0391-\uFFE5]";67 /*获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1*/

68 for (int i = 0; i < value.length(); i++) {69 /*获取一个字符*/

70 String temp = value.substring(i, i + 1);71 /*判断是否为中文字符*/

72 if(temp.matches(chinese)) {73 /*中文字符长度为2*/

74 valueLength += 2;75 } else{76 /*其他字符长度为1*/

77 valueLength += 1;78 }79 }80 returnvalueLength;81 }82

83 /**

84 * 字符串数组转换List85 *@paramitems86 *@return

87 */

88 public static ListstringsToList(String[] items) {89 List lists = new ArrayList();90 for(int i=0; i

96 /**

97 * 字符串填充,将sour使用fillStr前补或后补满len长度98 *@paramsour 待填充字符串,支持含有中文99 *@paramfillStr 填充数据100 *@paramlen 填充完整字符串长度101 *@paramisLeft 是否左补填充数据,否则右补填充数据102 *@return

103 */

104 public static String fill(String sour, String fillStr, int len, booleanisLeft){105 if (sour == null) {106 sour = "";107 }108 int fillLen = len -length(sour);109 String fill = "";110 for (int i=0; i

120 /**

121 * 字符串填充,中间填充122 *@paramsour 左侧待填充字符串,支持含有中文123 *@paramspace 填充的数据,一般为* 、空格、——124 *@paramlengh 填充完整字符串长度125 *@parammodelparams 右侧待填充字符串,支持含有中文126 *@return

127 */

128 public static String fillMiddle(String sour, String space, intlengh, String modelparams){129 String s, ss = null;130 int lenS =sour.length();131 int lenM =modelparams.length();132 //做非空处理

133 if(sour == null) {134 sour = "";135 }136 if(modelparams == null) {137 modelparams = "";138 }139 if (space == "") {140 space = " ";141 }142 //若输入为汉字,则长度取2倍

143 if (sour.matches("[\u4e00-\u9fa5]+")) {144 lenS = lenS*2;145 }146 if (modelparams.matches("[\u4e00-\u9fa5]+")) {147 lenM = lenM*2;148 }149 //若输入有数字则,总长度加3

150 if (sour.matches("[0-9]") && modelparams.matches("[0-9]")) {151 lengh = lengh + 4;152 }153 //长度保护

154 if (lengh < (lenS +lenM)) {155 new Exception("哥们,长度设置太小了");156 }157

158 s = fill(modelparams, space, lengh - lenS-lenM, true);159 ss = sour + s +"\n";160

161 returnss;162 }163

164

165 /**

166 * 字符串填充167 *168 *@paramstrData 待填充字符串,不支持含有中文169 *@paramnLen170 *@param

171 *@paramnOption172 * 0:左侧填充; 1:右侧填充; 2:两边填充173 *@return

174 */

175 public static String paddingString(String strData, intnLen, String subStr,176 intnOption) {177 inti, addCharLen;178

179 String strHead = "";180 String strEnd = "";181

182 i =strData.length();183 if (i >=nLen) {184 returnstrData;185 }186

187 switch(nOption) {188 case 0:189 addCharLen = (nLen - i) /subStr.length();190 for (i = 0; i < addCharLen; i++) {191 strHead +=subStr;192 }193 return strHead +strData;194 case 1:195 addCharLen = (nLen - i) /subStr.length();196 for (i = 0; i < addCharLen; i++) {197 strEnd +=subStr;198 }199 return strData +strEnd;200 case 2:201 addCharLen = (nLen - i) / (subStr.length() * 2);202 for (i = 0; i < addCharLen; i++) {203 strHead +=subStr;204 strEnd +=subStr;205 }206 return strHead + strData +strEnd;207 default:208 returnstrData;209 }210 }211

212 /**

213 * 整形转换成BCD型的字符串214 * 9转换成后将变成09,00 09215 * 19转换后将变成19, 00 19216 *@paramvalue217 *@parambytesNum218 * BCD字节个数219 *@return

220 */

221 public static String intToBcd(int value, intbytesNum) {222 switch(bytesNum){223 case 1:224 if (value >= 0 && value <= 99){225 return paddingString(String.valueOf(value),2,"0",0);226 }227 break;228 case 2:229 if (value >= 0 && value <= 999) {230 return paddingString(String.valueOf(value),4,"0",0);231 }232 break;233

234 case 3:235 if (value >= 0 && value <= 999) {236 return paddingString(String.valueOf(value),3,"0",0);237 }238 break;239 }240

241 return "";242 }243

244 /**

245 * Hex数据转换成字符串246 *247 *@paramvalue248 *@return

249 *@throwsUnsupportedEncodingException250 */

251 public static String hexToStr(String value) throwsUnsupportedEncodingException {252 return new String(BytesUtils.hexToBytes(value),"GBK");253 }254 /**

255 * Hex数据转换成字符串256 * encoding 为编码257 *@paramvalue258 *@return

259 *@throwsUnsupportedEncodingException260 */

261 public static String hexToStr(String value, String encoding) throwsUnsupportedEncodingException {262 return newString(BytesUtils.hexToBytes(value),encoding);263 }264

265 /**

266 * 字符串转换成Hex267 *268 *@paramvalue269 *@return

270 */

271 public staticString strToHex(String value) {272 returnBytesUtils.bytesToHex(BytesUtils.getBytes(value));273 }274

275 /**

276 * 往value中填充一个字符0 ,当数据长度正好为2的整数倍时,不填充277 *278 *@paramvalue279 *@paramoption280 * 0:往后填充 ;1:往前填充281 *@return

282 */

283 public static String paddingZeroToHexStr(String value, intoption) {284

285 if (value.length() % 2 == 0){286 returnvalue;287 }288

289 if (option == 0){290 return "0" +value;291 }292 else if (option == 1){293 return value + "0";294 }295 else{296 returnvalue;297 }298 }299

300 /**

301 * 判断是否是Hex格式数据302 *303 *@paramvalue304 *@return

305 */

306 public static booleancheckHexStr(String value) {307 inti;308 intlen;309

310 if (value == null) return false;311

312 len =value.length();313 if (len == 0) return false;314

315 for (i= 0;i= '0' && value.charAt(i) <= '9')||

317 (value.charAt(i) >= 'a' && value.charAt(i) <= 'f') ||

318 (value.charAt(i) >= 'A' && value.charAt(i) <= 'F'))){319 return false;320 }321 }322 return true;323 }324

325 /**

326 * 判断字符串是否是数字0-9327 *328 *@paramvalue329 *@return

330 */

331 /*public static boolean checkDigitStr(String value) {332 int i;333 int len;334

335 if (value == null) return false;336

337 len = value.length();338 if (len == 0) return false;339

340 for (i= 0;i '9') {342 return false;343 }344 }345 return true;346 }*/

347

348 /**

349 * Binary数据转换成Hex350 *351 *@paramvalue352 *@return

353 */

354 public staticString binaryToHex(String value) {355 inti,j,len;356 String result ="";357 char[] hexVocable = { '0', '1', '2', '3',358 '4', '5', '6','7',359 '8', '9', 'A', 'B',360 'C', 'D', 'E', 'F'};361 String[] binString = {"0000", "0001", "0010", "0011",362 "0100", "0101", "0110", "0111",363 "1000", "1001", "1010", "1011",364 "1100", "1101", "1110", "1111"};365 //System.out.println("value: " + value);

366

367 len =value.length();368 for(i=0; i

377 returnresult;378 }379

380 /**

381 * Hex数据转换成Binary382 *383 *@paramvalue384 *@return

385 */

386 public staticString hexToBinary(String value) {387 inti,j,len;388 String result ="";389 char[] hexVocable = { '0', '1', '2', '3',390 '4', '5', '6','7',391 '8', '9', 'A', 'B',392 'C', 'D', 'E', 'F'};393 String[] binString = {"0000", "0001", "0010", "0011",394 "0100", "0101", "0110", "0111",395 "1000", "1001", "1010", "1011",396 "1100", "1101", "1110", "1111"};397

398 len =value.length();399 for(i=0; i

408 returnresult;409 }410

411 /**

412 * 获取二进制字符串413 * 0x00 0x01 0x00 0x01 0x01转换成"01011"414 *@paramvalue415 *@return

416 */

417 public static String getBinaryString(byte[] value) {418 intlen;419 String result ="";420

421 len =value.length;422

423 for(int i=0;i

427 returnresult;428 }429 /**

430 * mmdd日期格式431 *@paramdate432 *@return

433 */

434 public static booleanisMatchDate(String date){435 if(date.length()!=4||date==null){436 return false;437 }438 String eL="(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-9])))";439 Pattern p =Pattern.compile(eL);440 Matcher m =p.matcher(date);441 boolean b =m.matches();442 returnb;443 }444

445 /**

446 * 删除字符串左侧所有字符ch447 *@param

448 *@return

449 */

450 public static String deleteLeftChar(String data, charch){451 int flag = 0;452 for(int i = 0;i

464 /**

465 * 删除字符串右侧所有字符ch466 *@param

467 *@return

468 */

469 public static String deleteRightChar(String data, charch){470

471 int flag =data.length();472 for(int i = data.length()-1;i>=0;i--){473 if(data.charAt(i) ==ch){474 flag =i;475 }else{476 break;477 }478 }479

480 data = data.substring(0,flag);481 returndata;482 }483

484

485 /**

486 * 将显示金额的字符串(带原点),转化为没有原点的字符串487 *488 * 示例:123.32 ---> 12332489 * 2. ----> 200490 * 2.3 ----> 230491 * 0.12 ---> 12492 * 0.02 ---> 2493 *@paramstr494 *@return

495 */

496 public staticString decimalWipeDot(String str){497

498 //拿到字符串

499 if (str.contains(".")) {500

501 //判断点后还有几位

502 int indexOf = str.indexOf(".");503 //用索引和长度进行判断

504 int length =str.length();505 int cha = length -indexOf;506 char charAt = str.charAt(indexOf - 1);507

508 switch(cha) {509 case 1:510 //小数后有0位--先去掉点,在加个00 eg: 2. --> 200 0.-->0

511 if (charAt == '0') {512 str= "0";513 }else{514 str = str.replace(".", "");515 str= str+"00";516 }517 break;518 case 2:519 //小数后有1位--先去掉点,在加个0 eg: 2.3 --> 230 0.1 -->10

520 if (charAt == '0') {521 str= str.charAt(indexOf + 1)+"0";522 }else{523 str = str.replace(".", "");524 str= str+"0";525 }526

527 break;528 case 3:529 //小数后有2位--直接去掉点 eg: 2.03 --> 203 0.12-->12 0.02 --> 2

530 char charAt2 = str.charAt(indexOf + 1);531 if (str.length() == 4) {532

533 if (charAt == '0') {534 if (charAt2 == '0') {535 str= str.substring(length - 1,length);536 }else{537

538 str= str.substring(indexOf + 1,length);539 }540 }else{541 str = str.replace(".", "");542 }543 }else{544 str = str.replace(".", "");545 }546

547 break;548 default:549 break;550 }551 }else{552 //没有点, 直接加00 eg: 23 ---> 2300 0--->0 0000023---> 2300 00000--->0553 //去掉前面多余的0 ,在判断这个数是否为0

554 if (str.equals("")) {555 str = "0";556 }557 int int1 =Integer.parseInt(str);558 str =String.valueOf(int1);559

560 if (int1 != 0){561 str = str+"00";562 }else{563 str = "0";564 }565 }566 returnstr;567 }568

569 /**

570 * 将金额的字符串(不带点) 转化为带点的金额字符串571 * 示例 12332 ---> 123.32572 * 200 ---> 2573 * 230 ----> 2.3574 * 12 ---> 0.12575 * 2 ---> 0.02576 *577 *578 *@paramstr579 *@return

580 */

581 public staticString decimalAddDot(String str){582 boolean isNegative = false;583 str = str.replace(" ","");584

585 if (str == null || str == "") {586 return "0.00";587 }588 //截取字符第一个字符

589 String sign = str.substring(0,1);590 if (sign.equals("-")) {591 isNegative = true;//标记为负数

592 str = str.substring(1,str.length());593 }else{594 //正数不作处理

595 }596

597 int length =str.length();598 if (length >= 3) { //200-->2.00

599 str = str.substring(0, length-2)+"."+str.substring(length-2, length);600 }else{601 switch(length) {602 case 2://20-->0.20 23 --> 0.23

603 str = "0."+str;604 break;605 case 1://2-->0.02

606 str = "0.0"+str;607 break;608 case 0:609 //说明没有 0.00

610 str = "0.00";611 break;612 default:613 break;614 }615 }616

617 if(isNegative) {618 return "-"+str;619 }else{620 returnstr;621 }622

623

624

625 }626 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值