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

          換位思考·····
          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

          主站蜘蛛池模板: 蓬莱市| 响水县| 周口市| 墨竹工卡县| 固阳县| 怀仁县| 买车| 濮阳市| 邯郸县| 闽侯县| 简阳市| 农安县| 老河口市| 赤城县| 苍南县| 水城县| 三原县| 迭部县| 民丰县| 集贤县| 如东县| 孟州市| 南昌县| 乌审旗| 寻甸| 共和县| 景东| 洛南县| 东源县| 龙井市| 奉贤区| 靖宇县| 会宁县| 北辰区| 息烽县| 包头市| 武定县| 乌拉特前旗| 敦煌市| 东明县| 苍梧县|