【JAVA】Java中常用的方法
Java中常用的方法
1.substring 字符串截取
substring方法常用来截取字符串
1.将20201010截取为:2020年10月10日,
实现代码:
public class test { public static void main(String[] args) { String testDate = "20201010"; String year = testDate.substring(0,4); System.out.println(year); String month = testDate.substring(4,6); System.out.println(month); String day = testDate.substring(6,8); System.out.println(day); System.out.print(year+"年"+ month + "月" +day + "日"); } }
2.将/nas/file/inquiryOCR/2021-05-11 截取为: /inquiryOCR/2021-05-11
public static void main(String[] args) { String absolutePath = "/nas/file/inquiryOCR/2021-05-11"; String path = absolutePath.substring(absolutePath.indexOf("/inquiryOCR")); System.out.println("path:" + path); }
2.replace replace方法常用来替换字符串 如将日期中的“-”替换为“/”
public class test { public static void main(String[] args) { System.out.println("将日期中的“-”替换为“/” "); String newdData = "2020-10-10"; System.out.println("替换前:" + newdData ); String replace = newdData.replace("-", "/"); System.out.println("替换后:" + replace); } }