無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          String 工具類的常用操作方法大全

          Posted on 2009-07-22 22:54 Gavin.lee 閱讀(646) 評論(0)  編輯  收藏 所屬分類: java SE & EE

          受傷了,今天才知道字符串可以按照通配符來用其他串替換掉通配符,還是PHP的同事,他一語中的,我查了一下,真的可以,所以系統的看了一下String類,都試了一下,不藏私:

          package com.Gavin.tools;

          import org.apache.log4j.Logger;


          public class StrFormat {

              
          private static Logger logger = Logger.getLogger(StrFormat.class);
              
          public static void main(String[] args) {
                  
                  String test 
          = "http://wap.%s.com/";
                  
                  String testStr 
          = String.format(test, "500wan");        //給定參數替換掉test中的通配符
                  
                  logger.debug(testStr);
                  
                  logger.debug(testStr.charAt(
          0));    //取出testStr的第一個字符
                  
                  logger.debug(testStr.compareTo(
          "http"));    //testStr與http比較字典序,被比較的在前的返回正整數,在后的負整數,當相等時,返回0
                  
                  logger.debug(testStr.concat(
          "trade/"));
                  
                  logger.debug(testStr.indexOf(
          "a"));
                  
                  logger.debug(testStr.indexOf(
          "p"4));
                  
                  logger.debug(String.valueOf(
          5));    //將其他類型轉換成String
                  
                  logger.debug(testStr.startsWith(
          "h"));    //testStr是否以h開頭
                  
                  logger.debug(testStr.startsWith(
          "w"7));    //testStr從第七個字符開始的子串是否以w開頭
                  
                  logger.debug(testStr.endsWith(
          "/"));    //testStr是否以/結尾
                  
                  
          //replace首個參數均為regex正則
                  logger.debug(testStr.replace("wap""www"));    //替換掉testStr所有的wap為www
                  
                  logger.debug(testStr.replaceAll(
          "\\w""T"));    //替換所有單個字符為T,\w 單獨字符 [a-zA-Z_0-9]
                  
                  logger.debug(testStr.replaceFirst(
          "w""R"));    //替換掉testStr中第一個w為R
                  
                  String arr[] 
          = testStr.split("\\.");    //按照指定正則來分隔testStr。api:split(String regex)
                  for (String a: arr) {
                      logger.debug(a);
                  }

                  
                  logger.debug(testStr.substring(
          7));
                  
                  logger.debug(testStr.substring(
          1117));    //左包含
                  
                  logger.debug(testStr.toUpperCase());    
          //toLowerCase();
                  
                  logger.debug(
          "500wan".matches("\\w+"));        //true
                  
                  logger.debug(testStr.lastIndexOf(
          "0"));        //反向開始0出現的位置
                  
                  logger.debug(testStr.lastIndexOf(
          "0"10));        //反向10個字符開始,0出現的個數    
              }

          }

          //    [DEBUG      0  -line:15  -content:http://wap.500wan.com/
          //    [DEBUG      0  -line:17  -content:h
          //    [DEBUG     15  -line:19  -content:18
          //    [DEBUG     15  -line:21  -content:http://wap.500wan.com/trade/
          //    [DEBUG     15  -line:23  -content:8
          //    [DEBUG     47  -line:25  -content:9
          //    [DEBUG     47  -line:27  -content:5
          //    [DEBUG     47  -line:29  -content:true
          //    [DEBUG     47  -line:31  -content:true
          //    [DEBUG     47  -line:33  -content:true
          //    [DEBUG     47  -line:35  -content:http://www.500wan.com/
          //    [DEBUG     47  -line:37  -content:TTTT://TTT.TTTTTT.TTT/
          //    [DEBUG     47  -line:39  -content:http://Rap.500wan.com/
          //    [DEBUG     47  -line:43  -content:http://wap
          //    [DEBUG     47  -line:43  -content:500wan
          //    [DEBUG     47  -line:43  -content:com/
          //    [DEBUG     47  -line:46  -content:wap.500wan.com/
          //    [DEBUG     47  -line:48  -content:500wan
          //    [DEBUG     47  -line:50  -content:HTTP://WAP.500WAN.COM/
          //    [DEBUG     62  -line:52  -content:true
          //    [DEBUG     62  -line:54  -content:13
          //    [DEBUG     62  -line:56  -content:-1

          主站蜘蛛池模板: 岐山县| 磐安县| 辽阳县| 仪征市| 麦盖提县| 绿春县| 美姑县| 古田县| 永胜县| 博野县| 潜山县| 中宁县| 景宁| 长春市| 泗阳县| 兰溪市| 鄂州市| 克拉玛依市| 弥勒县| 雅江县| 永胜县| 封开县| 阜阳市| 且末县| 滦平县| 洛川县| 思茅市| 湘潭县| 阿鲁科尔沁旗| 奈曼旗| 乌什县| 金乡县| 巨鹿县| 会同县| 卢龙县| 东至县| 广丰县| 武隆县| 沙河市| 车致| 宁津县|