[Original] string tool class -- find the unit string

Source: Internet
Author: User
package week01;import java.util.regex.Matcher;import java.util.regex.Pattern;/**** *  * 字符串帮助类 * @author csharper * @since 2014.10.08 * */public class StringHelper {    /***     * 找出单位字符串大小     * @param str 源字符串     * @return  单位字符串大小     */    public static int findUnitSize(String str)    {        String unit="";        int unitSize=0;        if(str==null||"".equals(str.trim()))        {            return 0;        }        for(int i=0;i<str.length()/2+1;i++)        {            unit=str.substring(0, i+1);            if(match(str, "^("+unit+")+$"))            {                /***大小比索引值大1**/                unitSize=i+1;                break;            }            else            {                continue;            }        }        return unitSize;    }    /***     * 找出单位字符串     * @param str 源字符串     * @return  单位字符串     */    public static String findUnitString(String str)    {        int unitSize=findUnitSize(str);        return unitSize==0?"":str.substring(0,unitSize);    }    /***     *      * @param str 源字符串     * @param regex 正则表达式     * @return 是否匹配     */    public static boolean match(String str,String regex)    {        Pattern pattern=Pattern.compile(regex);        Matcher matcher=pattern.matcher(str);        return matcher.find();    }    public static void main(String[] args) {        System.out.println(findUnitSize("123123123123"));        System.out.println(findUnitString("123123123123"));    }}

收集个人常用工具类,方便日后使用。

【原创】字符串工具类--找出单元字符串

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.