欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

判断字符串string是数字、json结构、xml结构

程序员文章站 2022-05-29 11:43:29
1 import org.json.JSONException; 2 import org.json.JSONObject; 3 import org.dom4j.DocumentException; 4 import org.dom4j.DocumentHelper; 5 6 public cla ......
 1 import org.json.jsonexception;   
 2 import org.json.jsonobject;   
 3 import org.dom4j.documentexception;   
 4 import org.dom4j.documenthelper;   
 5   
 6 public class stringtest {   
 7   
 8 /**  
 9 * @param args  
10 */   
11 public static void main(string[] args) {   
12 string string1 = "123";   
13 string string2 = "fdgsewe323";   
14 string string3 = "{/"task/":/"324vfsdfg/"}";   
15 string string4 = "<ww>3243</ww>";   
16 //  system.out.println("string1::number"+isnumber(string1));   
17 //  system.out.println("string2::number"+isnumber(string2));   
18 //  system.out.println("string3::json"+isjson(string3));   
19 //  system.out.println("string4::xml"+isxml(string4));   
20 //  system.out.println("string1::json"+isjson(string1));   
21 //  system.out.println("string2::xml"+isxml(string2));   
22 //  system.out.println("string3::number"+isnumber(string3));   
23 //  system.out.println("string4::xml"+isxml(string4));   
24   
25 system.out.println("string1::"+gettype(string1));   
26 system.out.println("string2::"+gettype(string2));   
27 system.out.println("string3::"+gettype(string3));   
28 system.out.println("string4::"+gettype(string4));   
29 }   
30   
31 public static string gettype(string string) {   
32 if (isnumber(string))   
33 return "number";   
34 else if (isjson(string))   
35 return "json";   
36 else if (isxml(string))   
37 return "xml";   
38 else   
39 return "string";   
40 }   
41   
42   
43 /**  
44 * 判断字符串是否是数字  
45 */   
46 public static boolean isnumber(string value) {   
47 return isinteger(value) || isdouble(value);   
48 }   
49   
50 /**  
51 * 判断字符串是否是整数  
52 */   
53 public static boolean isinteger(string value) {   
54 try {   
55 integer.parseint(value);   
56 return true;   
57 } catch (numberformatexception e) {   
58 return false;   
59 }   
60 }   
61   
62 /**  
63 * 判断字符串是否是浮点数  
64 */   
65 public static boolean isdouble(string value) {   
66 try {   
67 double.parsedouble(value);   
68 if (value.contains("."))   
69 return true;   
70 return false;   
71 } catch (numberformatexception e) {   
72 return false;   
73 }   
74 }   
75   
76 /**  
77 * 判断是否是json结构  
78 */   
79 public static boolean isjson(string value) {   
80 try {   
81 new jsonobject(value);   
82 } catch (jsonexception e) {   
83 return false;   
84 }   
85 return true;   
86 }   
87   
88 /**  
89 * 判断是否是xml结构  
90 */   
91 public static boolean isxml(string value) {   
92 try {   
93 documenthelper.parsetext(value);   
94 } catch (documentexception e) {   
95 return false;   
96 }   
97 return true;   
98 }   
99 }