posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

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

          受傷了,今天才知道字符串可以按照通配符來用其他串替換掉通配符,還是PHP的同事,他一語中的,我查了一下,真的可以,所以系統(tǒng)的看了一下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");        //給定參數(shù)替換掉test中的通配符
                  
                  logger.debug(testStr);
                  
                  logger.debug(testStr.charAt(
          0));    //取出testStr的第一個字符
                  
                  logger.debug(testStr.compareTo(
          "http"));    //testStr與http比較字典序,被比較的在前的返回正整數(shù),在后的負(fù)整數(shù),當(dāng)相等時,返回0
                  
                  logger.debug(testStr.concat(
          "trade/"));
                  
                  logger.debug(testStr.indexOf(
          "a"));
                  
                  logger.debug(testStr.indexOf(
          "p"4));
                  
                  logger.debug(String.valueOf(
          5));    //將其他類型轉(zhuǎn)換成String
                  
                  logger.debug(testStr.startsWith(
          "h"));    //testStr是否以h開頭
                  
                  logger.debug(testStr.startsWith(
          "w"7));    //testStr從第七個字符開始的子串是否以w開頭
                  
                  logger.debug(testStr.endsWith(
          "/"));    //testStr是否以/結(jié)尾
                  
                  
          //replace首個參數(shù)均為regex正則
                  logger.debug(testStr.replace("wap""www"));    //替換掉testStr所有的wap為www
                  
                  logger.debug(testStr.replaceAll(
          "\\w""T"));    //替換所有單個字符為T,\w 單獨(dú)字符 [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出現(xiàn)的位置
                  
                  logger.debug(testStr.lastIndexOf(
          "0"10));        //反向10個字符開始,0出現(xiàn)的個數(shù)    
              }

          }

          //    [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

          主站蜘蛛池模板: 临武县| 东城区| 云霄县| 新疆| 漳浦县| 特克斯县| 河北区| 金乡县| 镇远县| 穆棱市| 巫山县| 龙胜| 句容市| 常熟市| 兴山县| 武穴市| 丰顺县| 永福县| 永定县| 砀山县| 霍林郭勒市| 赣州市| 北川| 囊谦县| 肇庆市| 柏乡县| 仁怀市| 闽清县| 北川| 台安县| 开江县| 棋牌| 博白县| 丹寨县| 香河县| 乐清市| 崇明县| 汉川市| 抚州市| 全州县| 巨野县|