无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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

Posted on 2009-07-22 22:54 Gavin.lee 阅读(637) 评论(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


只有注册用户登录后才能发表评论。


网站导航: