File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .string ;
2
+
3
+ import org .apache .commons .lang .ObjectUtils ;
4
+ import org .apache .commons .lang .StringUtils ;
5
+ import org .apache .commons .lang .math .NumberUtils ;
6
+
7
+ /**
8
+ * @Author: EnjoyCoding
9
+ * @Date: 2020\3\30 0030 23:58
10
+ * @Description:
11
+ */
12
+ public class StringToIntegerDemo {
13
+
14
+
15
+ public static void main (String [] args ) {
16
+ // objectToString();
17
+ // nullToEmpty();
18
+ // strToInteger();
19
+ integerValueOfDemo ();
20
+ }
21
+
22
+ public static void nullToEmpty () {
23
+ Object object =null ;
24
+ String str = ObjectUtils .toString (object );
25
+ // String str = ObjectUtils.toString(object,null);
26
+ System .out .println ("值为:" +str );
27
+ }
28
+
29
+ public static void strToInteger () {
30
+ String str ="abc" ;
31
+ //str不为数字时,设置默认值为 0
32
+ int num = NumberUtils .toInt (str );
33
+ //str不为数字时,设置默认值为其他值
34
+ int defaultNum =NumberUtils .toInt (str ,123 );
35
+ }
36
+
37
+ public static void integerValueOfDemo () {
38
+ String str ="123" ;
39
+ // String str="";
40
+ //str为非数字字符串时,报错: NumberFormatException:""
41
+ Integer value = Integer .valueOf (str );
42
+ System .out .println (value );
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments