The NoteBook of EricKong

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(11)

          我參與的團(tuán)隊(duì)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          In this article we will see how to split string in Java both by using String’s split() method and StringTokenizer. If you have any doubt on anything related to split string please let me know and I will try to help.


          String Split Example Java
          Let's see an example of splitting string in Java by using split() function:

           

          //split string example


          String assetClasses 
          = "Gold:Stocks:Fixed Income:Commodity:Interest Rates";
          String[] splits 
          = asseltClasses.split(":");


          System.out.println(
          "splits.size: " + splits.length);


          for(String asset: assetClasses){
          System.out.println(asset);
          }



          OutPut
          splits.size: 5
          Gold
          Stocks
          Fixed Income
          Commodity
          Interest Rates

          In above example we have provided delimiter or separator as “:” to split function which expects a regular expression and used to split the string.
          Now let see another example of split using StringTokenizer

          //string split example StringTokenizer


          StringTokenizer stringtokenizer 
          = new StringTokenizer(asseltClasses, ":");
          while (stringtokenizer.hasMoreElements()) {
          System.out.println(stringtokenizer.nextToken());
          }



          OutPut
          Gold
          Stocks
          Fixed Income
          Commodity
          Interest Rates


          My personal favorite is String.split () because it’s defined in String class itself and its capability to handle regular expression which gives you immense power to split the string on any delimiter you ever need. Though it’s worth to remember following points about split method in Java


          1) Some special characters need to be escaped while using them as delimiters or separators e.g. "." and "|".

           //string split on special character “|”

          String assetTrading 
          = "Gold Trading|Stocks Trading|Fixed Income Trading|Commodity Trading|FX trading";


          String[] splits 
          = assetTrading.split("\\|");  // two \\ is required because "\"     itself require escaping


          for(String trading: splits){
          System.out.println(trading);
          }



          OutPut:
          Gold Trading
          Stocks Trading
          Fixed Income Trading
          Commodity Trading
          FX trading

           

           // split string on “.”
          String smartPhones = "Apple IPhone.HTC Evo3D.Nokia N9.LG Optimus.Sony Xperia.Samsung Charge”;


          String[] smartPhonesSplits 
          = smartPhones.split("\\.");


          for(String smartPhone: smartPhonesSplits){
          System.out.println(smartPhone);
          }


          OutPut:
          Apple IPhone
          HTC Evo3D
          Nokia N9
          LG Optimus
          Sony Xperia
          Samsung Charge

          2) You can control number of split by using overloaded version split (regex, limit). If you give limit as 2 it will only creates two strings. For example in following example we could have total 4 splits but if we just want to create 2 we can use limit.

           


          //string split example with limit


          String places 
          = "London.Switzerland.Europe.Australia";
          String[] placeSplits 
          = places.split("\\.",2);


          System.out.println(
          "placeSplits.size: " + placeSplits.length );


          for(String contents: placeSplits){
          System.out.println(contents);
          }



          Output:
          placeSplits.size: 2
          London
          Switzerland.Europe.Australia

          To conclude the topic StringTokenizer is old way of tokenizing string and with introduction of split since JDK 1.4 its usage is discouraged. No matter what kind of project you work you often need to split string in Java so better get familiar with these API.

           

          posted on 2011-12-14 13:44 Eric_jiang 閱讀(253) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 桑日县| 楚雄市| 启东市| 雷山县| 金阳县| 曲松县| 金乡县| 读书| 鸡东县| 手机| 安国市| 抚松县| 烟台市| 绥宁县| 晋宁县| 宝鸡市| 岳普湖县| 泰州市| 广西| 深水埗区| 唐山市| 南京市| 博湖县| 南召县| 通榆县| 白山市| 岑巩县| 黄山市| 富民县| 佳木斯市| 陵水| 浠水县| 家居| 二连浩特市| 武威市| 澎湖县| 西乌珠穆沁旗| 开平市| 武鸣县| 铜梁县| 马关县|