Java 简单实用方法二
2017-05-20 17:44
381 查看
整理以前的笔记,在学习Java时候,经常会用到一些方法。虽然简单但是经常使用。因此做成笔记,方便以后查阅
这篇博文先说明构造和使用这些方法。
1,判断String类型数据是否包含中文
可以通过正则表达式来判断。
2,获取String类型中文的数据
那么先通过正则表达式来判断是否包含中文,如果数据包含中问便将中文数据取出来。
注:使用StringBuffer累加比String 效率更高。
3,四舍五入取整数
使用DecimalFormat来格式化十进制数字。
注: 0 一个数字
# 一个数字,不包括 0
. 小数的分隔符的占位符
4,String数据中插入指定字符
方法一:使用substring方法拼接起来
方法二:使用StringBuffer中insert方法
5,String数据替换指定字符
方法一:通过在String插入字符可以得出,原该位置的字符回向后偏移,那么在拼接数据的时候去掉插入原位置的字符。
方法二:通过StringBuffer中的repace方法替换该字符
这篇博文先说明构造和使用这些方法。
1,判断String类型数据是否包含中文
可以通过正则表达式来判断。
public static boolean isChineseChar(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); Matcher m = p.matcher(str); return m.find(); } String str1="你好"; String str2="Hello"; String str3="你好 ,@¥Hello@…… 世界.\\ "; System.out.println(isChineseChar(str1)); //true System.out.println(isChineseChar(str2)); //false System.out.println(isChineseChar(str3)); //true
2,获取String类型中文的数据
那么先通过正则表达式来判断是否包含中文,如果数据包含中问便将中文数据取出来。
注:使用StringBuffer累加比String 效率更高。
public static String getChinese(String paramValue) { String regex = "([\u4e00-\u9fa5]+)"; StringBuffer sb=new StringBuffer(); Matcher matcher = Pattern.compile(regex).matcher(paramValue); while (matcher.find()) { sb.append(matcher.group(0)); } return sb.toString(); } String str1="你好"; String str2="Hello"; String str3="你好 ,@¥Hello@…… 世界.\\ "; System.out.println(getChinese(str1)); //你好 System.out.println(getChinese(str2)); // System.out.println(getChinese(str3)); //你好世界
3,四舍五入取整数
使用DecimalFormat来格式化十进制数字。
注: 0 一个数字
# 一个数字,不包括 0
. 小数的分隔符的占位符
public static Double formatDouble(Double db,String format) { if(null==format||"".equals(format)){ return db; } DecimalFormat df = new DecimalFormat(format); //定义格式 return Double.parseDouble(df.format(db)); } double db1=5.6849; System.out.println(formatDouble(db1,"#.00")); //5.68 System.out.println(formatDouble(db1,"#.000"));//5.685 System.out.println(formatDouble(db1,"#")); //6.0 System.out.println(formatDouble(db1,".00")); //5.68 System.out.println(formatDouble(db1,"00")); //6.0
4,String数据中插入指定字符
方法一:使用substring方法拼接起来
public static String insertString1(String a,String b,int t){ return a.substring(0,t)+b+a.substring(t,a.length()); } String str4="abcdef"; System.out.println(insertString1(str4,"p",2));//abpcdef System.out.println(insertString1(str4,"k",3));//abckdef
方法二:使用StringBuffer中insert方法
public static String insertString2(String a,String b,int t) { StringBuilder sb = new StringBuilder(a); sb.insert(t, b); return sb.toString(); } String str4="abcdef"; System.out.println(insertString2(str4,"p",2));//abpcdef System.out.println(insertString2(str4,"k",3));//abckdef
5,String数据替换指定字符
方法一:通过在String插入字符可以得出,原该位置的字符回向后偏移,那么在拼接数据的时候去掉插入原位置的字符。
public static String repaceString1(String a,String b,int t){ return a.substring(0,t)+b+a.substring(t+b.length(),a.length()); } String str4="abcdef"; System.out.println(repaceString1(str4,"pp",2));//abppef System.out.println(repaceString1(str4,"kk",3));//abckkf
方法二:通过StringBuffer中的repace方法替换该字符
public static String repaceString2(String a,String b,int t){ StringBuilder sb = new StringBuilder(a); sb.replace(t, t+b.length(), b); return sb.toString(); } String str4="abcdef"; System.out.println(repaceString2(str4,"pp",2));//abppef System.out.println(repaceString2(str4,"kk",3));//abckkf
相关文章推荐
- java的calendar的简单实用方法
- Java简单实用方法一
- 关于日期,java处理日期间隔的方法(简单实用)
- Java简单实用方法一
- Java-文件File简单实用方法(分享)
- Java 简单实用方法二
- Java画笔的简单实用方法
- 最简单的Java操作Excel方法
- 一种简单实用的全屏方法
- java中的集合类的几个鲜为人知的实用方法
- 一个简单实用的ASP调试函数和使用方法
- 一种简单的方法在程序中实现透明效果(JAVA)(源代码)
- Java - 简单加密-解密方法包装, 含encode(),decode(),md5()
- VC/MFC简单实用的全屏方法
- JAVA实用技巧:定时执行任务的三种方法
- Java中的集合类的几个鲜为人知的实用方法
- 汉字转换成Unicode,简单的java实现方法
- [JAVA] 一个用java读取XML文件的简单方法
- [转]VC/MFC简单实用的全屏方法
- 提高Java技能的几种简单有效的方法