|
|
@@ -0,0 +1,78 @@
|
|
|
+package com.zheng.common.util;
|
|
|
+
|
|
|
+/**
|
|
|
+ * String 工具类
|
|
|
+ * Created by shuzheng on 2016/12/07.
|
|
|
+ */
|
|
|
+public class StringUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * object转String
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getString(Object object) {
|
|
|
+ return getString(object, "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getString(Object object, String defaultValue) {
|
|
|
+ if (null == object) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return object.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * object转Integer
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getInt(Object object) {
|
|
|
+ return getInt(object, -1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getInt(Object object, Integer defaultValue) {
|
|
|
+ if (null == object) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(object.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * object转Boolean
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean getBoolean(Object object) {
|
|
|
+ return getBoolean(object, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean getBoolean(Object object, Boolean defaultValue) {
|
|
|
+ if (null == object) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Boolean.parseBoolean(object.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(getString(null));
|
|
|
+ System.out.println(getString(null, "hello"));
|
|
|
+
|
|
|
+ System.out.println(getInt("1"));
|
|
|
+ System.out.println(getInt("xx"));
|
|
|
+ System.out.println(getInt("xx", 4));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|