您的位置:首页 > 编程语言 > Java开发

Java常用方法函数总结

2016-08-25 22:39 399 查看
判断字符串为空的方法

public static boolean isNull(String... obj){
for(String s : obj){
if(s == null || "".equals(s)){
return true;
}
}
return false;
}


判断一个字符是否包含在一个数组字符中

private boolean isInStringArray(String string, ArrayList<String> arrayList) {
for (String oneString : arrayList) {
if (string.equals(oneString)) {
return true;
}
}
return false;
}


android判断EditText输入的数字、中文还是字母方法
String txt = edInput.getText().toString();

Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if(m.matches() ){
Toast.makeText(Main.this,"输入的是数字", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();
}


String txt = edInput.getText().toString();

Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if(m.matches() ){
Toast.makeText(Main.this,"输入的是数字", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();
}


华为手机管家黑名单判定流程

public static byte[] imsiToKey(String imsi) {
if (imsi == null) {
return null;
}
int len = imsi.length();
int lenKey = len / 2 + 1;
boolean even = len % 2 != 0;

byte[] key = new byte[lenKey];
for (int i = 0; i < lenKey; i++) {
if (i == 0) {
key[0] = (byte) (0x00 + (imsi.charAt(0) - '0') * 16 + 9);
} else if (i == (lenKey - 1) && !even) {
key[i] = (byte) (0x00 + 0xF0 + (imsi.charAt(len - 1) - '0'));
} else {
key[i] = (byte) (0x00 + (imsi.charAt(i * 2) - '0') * 16 + (imsi.charAt(i * 2 - 1) - '0'));
}
}

MTKlog.i(TAG, "imsiToKey is : " + key.toString());
return key;
}


转16进制

public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}


如何把 int、short 变量与 byte[] 的转换

private static byte[] intToByteArray(int data) {
return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data).array();
}

private static byte[] shortToByteArray(short data) {
return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(data).array();
}

private static short byteArrayToShort(byte[] b) {
return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
}

private static int byteArrayToInt(byte[] b) {
return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 函数