本文共 6450 字,大约阅读时间需要 21 分钟。
Java?????Java????
?????
?????????String ????????????????????????????????????????????????????????????????????????? String ???????????????????Regular Expression??? Regex?????????????
????????
????????????????????????????????????????????
class JavaAPIDemo { public static void main(String[] args) { String str = "123"; if (isNumber(str)) { int num = Integer.parseInt(str); System.out.println(num * 2); } } public static boolean isNumber(String str) { char data[] = str.toCharArray(); for (int x = 0; x < data.length; x++) { if (data[x] > '9' && data[x] < 'e') { return false; } } return true; }} ???????????????????????????????????????????????????
???????
?????????? Perl ???JDK 1.4 ????? Java ?????? java.util.regex ??String ???????????????
???????????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "asda$D%as18s1d5a/sd;22@!)odskdaPP0)"; String regex = "[^a-zA-Z0-9]+"; System.out.println(str.replaceAll(regex, "")); }} ?????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "a1258b125c147dd789eee258fff"; String regex = "\\d+"; String[] result = str.split(regex); System.out.println(Arrays.toString(result)); }} ??????????? double
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "100.1"; String regex = "\\d+(\\.\\d+)?"; System.out.println(str.matches(regex)); // true }} ?????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "1981-20-15"; String regex = "\\d{4}-\\d{2}-\\d{2}"; if (str.matches(regex)) { System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str)); } }} ????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "(010)-51283346"; String regex = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}"; System.out.println(str.matches(regex)); // true }} ????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "java_888@163.com"; String regex = "[a-zA-Z0-9]\\w+@\\w+\\.cn|com|com\\.cn|net|gov"; System.out.println(str.matches(regex)); // true }} Java util.regex ???
java.util.regex ???? Pattern ? Matcher ?????????????Pattern ??????????Matcher ??????????
????????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "JakdWEas51da&S&*ayas*(Ddada21321AD5da2@!!0iAS"; String regex = "[^a-zA-Z]+"; Pattern pattern = Pattern.compile(regex); String[] result = pattern.split(str); System.out.println(Arrays.toString(result)); // [JakdWEas, da, S, ayas, Ddada, AD, da, iAS] }} ????
public class JavaDemo01 { public static void main(String[] args) throws Exception { String str = "INSERT INTO dept(deptno, dname, loc) VALUES (#{deptno}, #{dname}, #{loc})"; String regex = "#\\{\\w+\\}"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group(0)); System.out.println(matcher.group(0).replaceAll("#|\\{|\\}", "")); } }} ???????
?????????????????? Locale ????????????Locale ?????????????ResourceBundle ??????????
Locale ?
Locale ????????????????????????????????
public Locale(String language)public Locale(String language, String country)
????????????? zh_CN ? en_US?
ResourceBundle ??????
ResourceBundle ?????????????? getBundle ? getString?????????? baseName_languageProperties?? cn.mldn.message.Messages_zh_CN.properties?
?????????
public class LocalTest { public static void main(String[] args) throws UnsupportedEncodingException { ResourceBundle resourceBundle = ResourceBundle.getBundle("cn.mldn.message.Messages"); String val = resourceBundle.getString("info"); System.out.println(val); }} ???????
?? MessageFormat ??????????? ResourceBundle ??????????????
???????????
public class LocalTest { public static void main(String[] args) throws UnsupportedEncodingException { Locale locale = Locale.CHINA; ResourceBundle resourceBundle = ResourceBundle.getBundle("cn.mldn.message.Messages", locale); String val = resourceBundle.getString("info"); System.out.println(MessageFormat.format(val, "liming", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))); }} ??????
UUID ?
UUID ??????????????????????
????? UUID
public class UUIDTest { public static void main(String[] args) { System.out.println(UUID.randomUUID()); UUID uuid = UUID.randomUUID(); System.out.println(uuid.toString()); }} Optional ?
Optional ????? null ????????????????
??????????
public class JavaAPI { public static void main(String[] args) { IMessage temp = MessageUtil.getMessage().orElse(new MessageImpl()); MessageUtil.useMessage(temp); }} ThreadLocal ?
ThreadLocal ????????????????????
????????????
public class JavaAPI { public static void main(String[] args) { new Thread(() -> { Message message = new Message(); message.setInfo("???????"); Channel.setMessage(message); Channel.send(); }, "?????A").start(); new Thread(() -> { Message message = new Message(); message.setInfo("???????"); Channel.setMessage(message); Channel.send(); }, "?????B").start(); new Thread(() -> { Message message = new Message(); message.setInfo("???????"); Channel.setMessage(message); Channel.send(); }, "?????C").start(); }} Base64 ?????
Java 8 ??? Base64 ???????????
???Base64 ?????
public class Base64Test { public static void main(String[] args) { String msg = "1234567890"; String enMsg = new String(Base64.getEncoder().encode(msg.getBytes())); System.out.println(enMsg); String oldMsg = new String(Base64.getDecoder().decode(enMsg)); System.out.println(oldMsg); }} ??
??????????? Java ??????????????????????????????????????? UUID?Optional?ThreadLocal ??????????????????????????????????????????????????????
发表评论
最新留言
关于作者