//indexOf/contains/StringBuffer/substring//字符串是否包含某个字符String s ="abcdefg";if(s.indexOf("a")==-1){System.out.println("字符串不包含此字符!");}else{//字符串包含此字符!System.out.println("字符串包含此字符!");}if(!s.contains("a")){System.out.println("字符串不包含此字符!");}else{//字符串包含此字符!System.out.println("字符串包含此字符!");}//字符串长度System.out.println(s.length());//7//字符串拼接StringBuffer sbf =newStringBuffer();
sbf.append("1234567");
sbf.append("1");System.out.println("拼接后"+sbf);//拼接后12345671
sbf.delete(0,3);System.out.println("删除后"+sbf);//删除后45671//字符串截取转日期String str ="20220203";String year = str.substring(0,4);String month = str.substring(4,6);String day = str.substring(6,8);SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd");Object da = sdf.parse(year+"-"+month+"-"+day);System.out.println(da);//Thu Feb 03 00:00:00 CST 2022String dd = sdf.format(da);System.out.println(dd);//2022-02-03//indexOf结合substring截取字符串以及replace的使用String a ="辽宁_沈阳";System.out.println(a.indexOf("_"));String b = a.substring(0,a.indexOf("_"));System.out.println(b);System.out.println(a.replace("_","-"));
//字符串转换double计算String a ="12";String b ="26";System.out.println(Double.parseDouble(a)/Double.parseDouble(b));//0.46153846153846156//保留两位小数DecimalFormat df =newDecimalFormat("#####0.00");System.out.println(df.format(Double.parseDouble(a)/Double.parseDouble(b)));//0.46