java工具类代码笔记(判断中文、整数、正负数、获取服务器ip等...)
1.判断是否是整数,包含正数和负数
/** * 判断是否是整数包含正负 */ public static boolean isInteger(String str){ Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$"); return pattern.matcher(str).matches(); }
2.判断是否包含汉字:
1 package com.test.character; 2 3 public class TestCharacter { 4 5 public static void main(String[] args) { 6 System.out.println(isChinese("我是abc")); 7 System.out.println(isChinese("i am in china")); 8 } 9 10 /** 11 * 是否包含汉字 12 * 13 * @param c 14 * @return 15 */ 16 public static boolean isChinese(char c) { 17 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); 18 if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS 19 || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS 20 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A 21 || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION 22 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION 23 || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { 24 return true; 25 } else { 26 return false; 27 } 28 } 29 30 public static boolean isChinese(String str) { 31 char[] chars = str.toCharArray(); 32 for (char c : chars) { 33 if (isChinese(c)) { 34 return true; 35 } 36 } 37 return false; 38 } 39 40 }
Character.UnicodeBlock.of()方法介绍:https://www.nhooo.com/note/qa0z42.html
Character.UnicodeBlock中的CJK说明:https://blog.csdn.net/yangdan1025/article/details/86586578
3.计算字符串的字节长度
package com.test.stringformat; import java.io.UnsupportedEncodingException; public class TestStrLength { public static void main(String[] args) { String str = "你看这串字符串多长abc"; System.out.println(calStrLength(str)); //30 } /** * 计算字符串的字节长度,中文占3个字节(UTF-8格式) * * @param str * @return */ public static int calStrLength(String str) { int length = 0; try { length = str.getBytes("utf-8").length; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return length; } }

4.获取xml等标签块中的值
public class XmlUtil {
public static String getNodeText(String xmlString, String nodeName) {
String beginName = "<" + nodeName + ">";
String endName = "</" + nodeName + ">";
int beginIndex = xmlString.indexOf(beginName);
if(beginIndex == -1){
return "";
}else {
int endIndex = xmlString.indexOf(endName);
String nodeValue = xmlString.substring(beginIndex + beginName.length(), endIndex);
return nodeValue;
}
}
}
5.获取服务端IP、获取ip:
package com.common;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* @description 常用用具类
* @date: 2024-12-28 13:30
*/
public class Util {
/**
* 获取服务端IP
* @return
*/
public static String getServerIp() {
String ip = null;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface currentInterface = interfaces.nextElement();
if (!currentInterface.isUp() || currentInterface.isLoopback() || currentInterface.isVirtual()) {
continue;
}
Enumeration<InetAddress> addresses = currentInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress currentAddress = addresses.nextElement();
if (currentAddress.isLoopbackAddress() || !currentAddress.getHostAddress().contains(":")) {
ip = currentAddress.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
public static void main(String[] args) {
String ip = Util.getServerIp();
System.out.println(ip);
}
}
---
浙公网安备 33010602011771号