如何在Java中拆分字符串


注:本文轉自《白煮蛋的博客》  

這里有兩種實現方式。

方式1:因為你必須用一個特殊字符分割兩個數字,你可以使用正則表達式

import java.util.regex.Matcher; import java.util.regex.Pattern; public class TrialClass { public static void main(String[] args) { Pattern p = Pattern.compile("[0-9]+"); Matcher m = p.matcher("004-034556"); while(m.find()) { System.out.println(m.group()); } } } 

方式2:使用字符串拆分方法

public class TrialClass { public static void main(String[] args) { String temp = "004-034556"; String [] arrString = temp.split("-"); for(String splitString:arrString) { System.out.println(splitString); } } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM