StringUtil之String工具类

获取字典字符串中的value <功能详细描述>
public static final String dictToValue(String mString) {

* 获取字典字符串中的ID <功能详细描述>

public static final String dictToID(String mString)

* 是否为null或空字符串
public static boolean isEmpty(String str) {

* 字符串转为整数(如果转换失败,则返回 -1)
public static int stringToInt(String str)

public static Long stringToLong(String str, Long defaultValue)

/** 字符串转日期 **/
public static Date StrToDate(String str)

public static int stringToInt(String str, int DefaultValue)

* 字体串转为boolean (如果转换失败,则返回false)
public static boolean stringToBoolean(String str) {

* boolean转为字体串

public static String BooleanToString(Boolean bool)

* 生成N位随机数

public static String get10Random(int num)

* 从异常中获取有用信息

public static String getExceptionMsg(Throwable ex)

/** 将文本数据复制到剪贴板 **/
public static void copyText(Context context, String message)

* 通过文件名,获取文件后缀
public static String getFileSuffix(String fullPath)

* 通过文件名,获取文件名称

public static String getFileName(String fullPath)

* 通过文件名,获取文件名称但没有后缀

public static String getFileNameNoFileSuffix(String fullPath)

* 截取时间日期
public static String getFilterDate(String data) {

/**截取字符串 **/
public static String getFilterNum(String num)

/**截取字符串 **/

public static String getTrueString(String num)

*获取两位的小数点的字符串

public static String getTwoString(String number)

package com.dfsw.contract.utils;import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;import android.content.Context;
import android.text.ClipboardManager;/*** 字符串操作 <功能详细描述>* * @author* @version* @see [相关类/方法]* @since [产品/模块版本]*/
public class StringUtil {private StringUtil() {}/*** 获取字典字符串中的value <功能详细描述>* * @param 输入的字典字符串* @return [参数说明]字符串中的值* @return String [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static final String dictToValue(String mString) {if (mString != null && mString.length() >= 1 && mString.contains("#")) {String[] strings = mString.split("#");return strings[1];} else {return "";}}/*** 获取字典字符串中的ID <功能详细描述>* * @param 输入的字典字符串* @return [参数说明]字符串中的ID* @return String [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static final String dictToID(String mString) {if (mString != null && mString.length() >= 1 && mString.contains("#")) {String[] strings = mString.split("#");return strings[0];} else {return "";}}/*** 是否为null或空字符串* * @param str* @return [参数说明]* * @return boolean [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static boolean isEmpty(String str) {if (null == str || "".equals(str)) {return true;}return false;}/*** 字符串转为整数(如果转换失败,则返回 -1)* * @param str* @return [参数说明]* * @return int [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static int stringToInt(String str) {return stringToInt(str, -1);}public static Long stringToLong(String str, Long defaultValue) {if (isEmpty(str)) {return defaultValue;}try {return Long.parseLong(str.trim());} catch (NumberFormatException e) {return defaultValue;}}/** 字符串转日期 **/public static Date StrToDate(String str) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = null;try {date = format.parse(str);} catch (ParseException e) {e.printStackTrace();}return date;}public static int stringToInt(String str, int DefaultValue) {if (isEmpty(str)) {return DefaultValue;}try {return Integer.parseInt(str.trim());} catch (NumberFormatException e) {return DefaultValue;}}/** 字符串转化成Float **//** public static Long stringToLong(String str, long l) { if (isEmpty(str)) {* return l; } String reg="^([-]|[0-9])[0-9]*(\\.\\w*)?$"; Matcher m =* Pattern.compile(reg).matcher(str); if(m.groupCount()>0) return* Long.parseLong(str); return l; }*//*** 字体串转为boolean (如果转换失败,则返回false)* * @param str* @return [参数说明]* * @return boolean [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static boolean stringToBoolean(String str) {if (isEmpty(str)) {return false;}try {return Boolean.parseBoolean(str.trim());} catch (Exception e) {return false;}}/*** boolean转为字体串* * @param str* @return [参数说明]* * @return boolean [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static String BooleanToString(Boolean bool) {String booleanString = "false";if (bool) {booleanString = "true";}return booleanString;}/*** 生成N位随机数* * @param num*            长度* @return [参数说明]* * @return String [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static String get10Random(int num) {Random random = new Random();StringBuffer res = new StringBuffer();for (int i = 0; i < num; i++) {res.append(random.nextInt(10));}return res.toString();}/*** 从异常中获取有用信息* * @param ex* @return [参数说明]* * @return String [返回类型说明]* @exception throws [违例类型] [违例说明]* @see [类、类#方法、类#成员]*/public static String getExceptionMsg(Throwable ex) {Writer writer = new StringWriter();PrintWriter printWriter = new PrintWriter(writer);ex.printStackTrace(printWriter);Throwable cause = ex.getCause();while (cause != null) {cause.printStackTrace(printWriter);cause = cause.getCause();}printWriter.close();String result = writer.toString();return result;}/** 将文本数据复制到剪贴板 **/public static void copyText(Context context, String message) {ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);cm.setText(message);}/*** 通过文件名,获取文件后缀* * @param fullPath* @return*/public static String getFileSuffix(String fullPath) {String result = "";int len = fullPath.length();int lastIndex = fullPath.lastIndexOf(".");if (len > lastIndex)result = fullPath.substring(lastIndex);return result;}/*** 通过文件名,获取文件名称* * @param fullPath* @return*/public static String getFileName(String fullPath) {String result = fullPath;int len = fullPath.length();int lastIndex = fullPath.lastIndexOf("/");if (len > lastIndex)result = fullPath.substring(lastIndex + 1);return result;}/*** 通过文件名,获取文件名称但没有后缀* * @param fullPath* @return*/public static String getFileNameNoFileSuffix(String fullPath) {String result = "";int len = fullPath.length();int lastIndex = fullPath.lastIndexOf("/");if (len > lastIndex)result = fullPath.substring(lastIndex + 1);if (!StringUtil.isEmpty(result)) {//len = result.length();int index = result.lastIndexOf(".");if (len > index) {result = result.substring(0, index - 1);}}return result;}/*** 截取时间日期*/public static String getFilterDate(String data) {String fiterdate = "";if (data != null && !"".equals(data)) {int index = data.indexOf("T");fiterdate = data.substring(0, index);}return fiterdate;}/**截取字符串 **/public static String getFilterNum(String num){String fiternum = "";if (num != null && !"".equals(num)) {int index = num.indexOf(".") + 3;fiternum = num.substring(0, index);}return fiternum;}/**截取字符串 **/public static String getTrueString(String num){String fiternum = "";if (num != null && !"".equals(num)) {fiternum = num.substring(1, num.length()-1);}return fiternum;}/**** *获取两位的小数点的字符串*/public static String getTwoString(String number){Double num = Double.parseDouble(number);//转换成DoubleDecimalFormat df = new DecimalFormat("0.00");//格式化String realNumber = df.format(num);return realNumber;}public static void main(String[] args) {System.out.println(get10Random(5));System.out.println(get10Random(10));System.out.println(get10Random(8));}
}

java StringUtil之String工具类相关推荐

  1. 【Java 代码实例 13】Java操作pdf的工具类itext

    目录 一.什么是iText? 二.引入jar 1.项目要使用iText,必须引入jar包 2.输出中文,还要引入下面```itext-asian.jar```包 3.设置pdf文件密码,还要引入下面` ...

  2. java图片缩放工具类,一个JAVA图形缩放处置工具类

    一个JAVA图形缩放处理工具类 调用的例子 import java.io.FileOutputStream; import java.io.IOException; import javax.imag ...

  3. JAVA I/O流工具类TextFile

    JAVA I/O流工具类TextFile由广州疯狂软件java培训分享: 本文是一个TextFile类,通过这个类我们可以调用其中的方法来简化对文件的读写,这段代码的可用性比较强.这个TextFile ...

  4. java redis remove_最全的Java操作Redis的工具类

    RedisUtil 当前版本:1.1 增加更全的方法,对以前的部分方法进行了规范命名,请放心替换成新版本. 介绍 最全的Java操作Redis的工具类,使用StringRedisTemplate实现, ...

  5. java图形验证码生成工具类

    转载自   java图形验证码生成工具类 生成验证码效果       ValidateCode.java 验证码生成类 package cn.dsna.util.images; import java ...

  6. java 代理ip工具类_Java基础之java处理ip的工具类

    java处理ip的工具类,包括把long类型的Ip转为一般Ip类型.把xx.xx.xx.xx类型的转为long类型.根据掩码位获取掩码.根据 ip/掩码位 计算IP段的起始IP.根据 ip/掩码位 计 ...

  7. java Excel导入导出工具类 及使用demo

    java Excel导入导出工具类 及使用demo 前言:相信进来的都是想尽快解决问题的,话不多说,按照以下步骤来,可以操作导出excel到本地,导入同理,自行学习.步骤一:直接复制以下excel工具 ...

  8. Java 数字转汉字工具类

    Java 数字转汉字工具类 一.工具类--NumberToCnUtil package com.example.demotest.util;import java.util.Arrays; impor ...

  9. java中常用的工具类

    1. 常用零散工具类 1.1[DateUtil.java]日期处理的工具类 /*** 时间日期处理工具* String -> Date* Date -> String* 以及生成含有日期的 ...

最新文章

  1. jq处理 php数组,jQuery数组处理方法汇总_jquery
  2. python操作符笔记
  3. 从 WordCount 到 MapReduce 计算模型
  4. P3387-【模板】缩点【tarjan,强联通分量,DAGdp】
  5. Android adb logcat使用技巧
  6. 动态规划求解限时采药问题(洛谷P1048题题解,Java语言描述)
  7. 知识图谱入门2-1:实践——基于医疗知识图谱的问答系统
  8. jenkind + git + mave + shell + tomcat
  9. 看不到日志_迷之 crontab 异常:不运行、不报错、无日志?
  10. 理解JVM(五):Java内存模型与线程
  11. PG数据库插件扩展搭建(一)
  12. 1563页Go语言中文文档,涵盖Go语言所有核心知识点
  13. GeoServer style(sld)中文乱码解决方法
  14. BeanUtils.copyProperties 和 fastjson 性能对比
  15. 5款神仙插件,打工人用了效率提升5倍
  16. ajax鼠标悬停,mouseout后触发jQuery Ajax鼠标悬停事件
  17. 分析黑客入侵 PostgreSQL 数据库
  18. 【MineCraft】-- 使用Java开发我的世界Mod
  19. 网络通讯端口为什么要设计浪涌保护电路
  20. How I Used a JSON Deserialization Oday to Steal Your Money on the Blockchain

热门文章

  1. 基于FPGA的ASCII码日期转时间戳算法实现
  2. 抖音小程序实践二:常用权限申请
  3. 【win10】开始菜单输cmd、运行、控制面板后点击无反应,win+x无反应,开始菜单右键无反应
  4. python3 子进程和父进程
  5. 丰县中等专业学校计算机专业,江苏省丰县中等专业学校2020
  6. 支付宝官方接口配置教程
  7. nvcc编译器之编译选项(chapter 4)
  8. maven打包报错Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.2.RELEASE:repa
  9. 【通州建设】地铁S6线更名为21号线!途径通州多站!
  10. 计算机操作系统(八)——并发程序设计