字符串相关

java_判断是否中文字符

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
public class IsChineseOrEnglish{
//GENERAL_PUNCTUATION 判断中文的“号
//CJK_SYMBOLS_and_punctuation 判断中文的。号
//HALFWIDTH_AND_FULLWIDTH_FORMS 判断中中文的,号

public static boolean isChinese(Char c){
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if(ub == Character.UnicodeBlock.CJK_SYMBOLS_and_punctuation
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_and_punctuation
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
return true;
}
return false;
}

public static void isChiese(String strName){
char[] ch = strName.toCharArray();
for(int i = 0; i < ch.length; i++){
char c = ch[i];
if(isChinese(c) == true){
System.out.println(isChiese(c));
return;
} else {
System.out.println(isChiese(c));
return;
}
}
}

public static void main(String[] args){
Random r = new Random();
for(int i = 0; i < 20; i++){
System.out.println(r.nextInt(10) + 1);
isChinese("き");
isChinese("中国");
}
}
}

验证日期格式的正确性

1
2
3
4
5
6
private boolean isDateFormatCorrect(String dataTime){
String el="^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
Pattern pattern = Pattern.compile(el);
Matcher matcher = pattern.matcher(dataTime);
return matcher.matches();
}

可以验证yyyy(\s/-)?MM(\s/-)?dd(\shh:mm:ss)?

String 转 Date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 字符串转Date
* @param format yyyy-MM-dd HH:mm:ss.SSS
* @param dateStr 要转换的字符串
* @return
*/
public static Date strToDate(String format, String dateStr){
Date date = null;
try {
date = new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}

Date 转 String

1
2
Date ddate;  
String sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate);

俩时间比较大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@SuppressLint("SimpleDateFormat")
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private boolean startCompareToEnd(String startDate, String endDate){
if(startDate == null || "".equals(startDate) || "null".equals(startDate)
|| endDate == null || "".equals(endDate) || "null".equals(endDate)){
return false;
}
//FIXME:输入字符串的格式与df格式比较下

try {
Date start = df.parse(startDate);
Date end = df.parse(endDate);
if(start.getTime() >= end.getTime()){
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}

随机生成数字

1
2
3
4
5
6
7
8
9
10
11
12
//生成6位随机数字
System.out.println((int)((Math.random()*9+1)*100000));
//生成5位随机数字
System.out.println((int)((Math.random()*9+1)*10000));
//生成4位随机数字
System.out.println((int)((Math.random()*9+1)*1000));
//生成3位随机数字
System.out.println((int)((Math.random()*9+1)*100));
//生成2位随机数字
System.out.println((int)((Math.random()*9+1)*10));
//生成1位随机数字
System.out.println((int)((Math.random()*9+1)));

字符串:截取pre和end之间的内容

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
public class Test {
public static void main(String[] args){
String url = "https://sbc2h5.laidy.cn/channelStoreList?easyChannelId=1252&storeId=123";
String url2 = "https://sbc2h5.laidy.cn/channelStoreList?easyChannelId=1252";
String url3 = "https://sbc2h5.laidy.cn/channelStoreList?storeId=123";

String easyChannelIdPre = "easyChannelId=";
String end = "&";
String storeIdPre = "storeId=";

System.out.println(getContent(url, easyChannelIdPre, end));
System.out.println(getContent(url, storeIdPre, end));
System.out.println("========================" + url);

System.out.println(getContent(url2, easyChannelIdPre, end));
System.out.println(getContent(url2, storeIdPre, end));
System.out.println("========================" + url2);

System.out.println(getContent(url3, easyChannelIdPre, end));
System.out.println(getContent(url3, storeIdPre, end));
System.out.println("========================" + url3);

String url4 = "/talk2Guide?guideId=1234&guiderLogo=httsdflkj&nick=5678";
String guideIdPre = "guideId=";
String guiderLogoPre = "guiderLogo=";
String guiderNickPre = "nick=";
String guideEnd = "&";
System.out.println(getContent(url4, guideIdPre, guideEnd));
System.out.println(getContent(url4, guiderLogoPre, guideEnd));
System.out.println(getContent(url4, guiderNickPre, guideEnd));
System.out.println("========================" + url4);
}

/**
* 截取 pre和end之间的内容
* @param url
* @param pre
* @param end
* @return
*/
private static String getContent(String url, String pre, String end){
if (url.contains(pre)){
url = url.substring(url.indexOf(pre));
if (url.indexOf(end) > 0 && url.indexOf(end) > url.indexOf(pre)){
return url.substring((url.indexOf(pre) + pre.length()), url.indexOf(end));
} else {
return url.substring((url.indexOf(pre) + pre.length()), url.length());
}
}
return null;
}
}

字符串的宽高

1
2
3
4
5
Rect rect=new Rect();
paint.getTextBounds(string, 0, string.length(), rect);

int strWidth=rect.width();
int strHeight=rect.height();