1
/*
2
* StringUtils.java
3
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
*/
16
package org.lambdasoft.utils;
17
18
import java.text.SimpleDateFormat;
19
import java.util.ArrayList;
20
import java.util.Date;
21
import java.util.List;
22
import java.util.StringTokenizer;
23
24
import org.apache.commons.logging.Log;
25
import org.apache.commons.logging.LogFactory;
26
27
/**
28
* 字符串處理工具
29
*
30
* @author TangLei <justinlei@gmail.com>
31
* @date 2008-11-22
32
*/
33
public class StringUtils {
34
private final static Log log = LogFactory.getLog(StringUtils.class);
35
private StringUtils(){}
36
37
/**
38
* 判斷字符串是否為空
39
*
40
* @param str 需要判斷的字符串
41
* @return booleanValue 返回是否為空
42
*/
43
public final static boolean isEmpty(String str) {
44
if(str == null)
45
return true;
46
str = str.trim();
47
if(str.length() == 0)
48
return true;
49
return false;
50
}
51
52
/**
53
* 判斷字符串是否為空
54
*
55
* @param str 需要判斷的字符串
56
* @return booleanValue 返回是否為空
57
*/
58
public final static boolean isNotEmpty(String str) {
59
return ! isEmpty(str);
60
}
61
62
public final static long getLength(String str) {
63
if(isEmpty(str))
64
return 0;
65
return str.trim().length();
66
}
67
68
/**
69
* 獲取分割的字符串
70
*
71
* @param str 需要分割的字符串
72
* @param segment 分割字符串
73
* @return segments 分割好的字符串數組
74
*/
75
public final static String[] getStringSegment(String str,String segment) {
76
if(str == null || str.trim().length() == 0)
77
return null;
78
if(segment == null || segment.length() == 0)
79
return null;
80
StringTokenizer stringTokenizer = new StringTokenizer(str,segment);
81
List<String> segs = new ArrayList<String>();
82
while(stringTokenizer.hasMoreTokens()) {
83
String _token = stringTokenizer.nextToken();
84
segs.add(_token);
85
if(log.isDebugEnabled()) {
86
log.debug("StringTokenizer.nextToken : " + _token);
87
}
88
}
89
String[] returns = new String[segs.size()];
90
for (int i = 0; i < segs.size(); i++) {
91
returns[i] = segs.get(i);
92
}
93
return returns;
94
}
95
96
/**
97
* 時間格式化默認為(yyyy-MM-dd HH:mm:ss)
98
*
99
* @param date
100
* @param expression
101
* @return
102
*/
103
public static final String formatDate(Date date,String expression) {
104
if(isEmpty(expression))
105
expression = "yyyy-MM-dd HH:mm:ss";
106
SimpleDateFormat sdf = new SimpleDateFormat(expression);
107
return sdf.format(date);
108
}
109
}
110

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110
