com.feilong.core.lang.StringUtilTemp.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.lang.StringUtilTemp.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.lang;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.junit.Assert.assertEquals;

import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.bean.ConvertUtil;
import com.feilong.tools.slf4j.Slf4jUtil;

import static com.feilong.core.Validator.isNullOrEmpty;

/**
 * The Class StringUtilTest.
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 */
public class StringUtilTemp {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(StringUtilTemp.class);

    /**
     * ??,232,???232.
     */
    @Test
    public void testStringUtilTest() {
        for (int i = 100; i <= 999; ++i) {
            if (i / 100 == i % 10) {
                System.out.println(i);
            }
        }
    }

    /**
     * Length.
     */
    @Test
    public void length() {
        String string = "?:http://weibo.com/venusdrogon,[url=http://bbs.guqu.net/Query.asp?keyword=%B6%C5%B4%CF%D7%A8%BC%AD&boardid=0&sType=2]sssss[/url][url=http://weibo.com/venusdrogon][img]http://service.t.sina.com.cn/widget/qmd/1903991210/1c853142/5.png[/img][/url]";
        LOGGER.debug(string.length() + "");

        // ???
        LOGGER.debug("3999e85461ce7271dd5292c88f18567e".length() + "");
    }

    /**
     * ? .
     * 
     * <pre class="code">
     * StringUtil.searchTimes("xin", "xin")     = 1
     * StringUtil.searchTimes("xiiiin", "ii")   = 2
     * </pre>
     *
     * @param source
     *            ?
     * @param target
     *            ?
     * @return count of target string in source
     * @see org.apache.commons.lang3.StringUtils#countMatches(CharSequence, CharSequence)
     * @since 1.0.2
     * @deprecated  {@link org.apache.commons.lang3.StringUtils#countMatches(CharSequence, CharSequence)}
     */
    @Deprecated
    private static int searchTimes(String source, String target) {
        Validate.notNull(source, "source can't be null!");
        Validate.notNull(target, "target can't be null!");
        // times 
        int count = 0;
        // while  
        int j = 0;
        // ??
        int fromIndex = 0;
        int sourceLength = source.length();
        //  0
        while (j != sourceLength) {
            //  ?
            int i = source.indexOf(target, fromIndex);
            if (i != StringUtils.INDEX_NOT_FOUND) {
                int targetLength = target.length();
                // ? , 
                //    
                j = i + targetLength;
                fromIndex = i + targetLength;
                count++;
            } else {
                // ?? 
                break;
            }
        }
        return count;
    }

    /**
     * ?.150?..
     * 
     * @return the random string
     */
    private static String getRandomString() {
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        int length = 150 + r.nextInt(50);
        for (int i = 0; i < length; i++) {
            sb.append('a' + r.nextInt(26));
        }
        return sb.toString();
    }

    // [start]startsWith

    /**
     * ?? <code>prefix</code>.
     * 
     * @param value
     *            value
     * @param prefix
     *            ?
     * @return ????, true;? false.<br>
     *         ??,?, String( equals(Object) ), true.
     */
    public static boolean startsWith(CharSequence value, String prefix) {
        return ConvertUtil.toString(value).startsWith(prefix);
    }

    // [end]

    /**
     * [?]:?(?)??.
     * 
     * <p>
     * {@link #substring(String, String, int)},  shift=0 ?? beginString.
     * </p>
     * 
     * <pre class="code">
     * substring("jinxin.feilong",".")  =.feilong
     * </pre>
     * 
     * @param text
     *            text
     * @param beginString
     *            beginString?
     * @return {@link #substring(String, String, int)},  shift=0 ?? beginString.
     * @see #substring(String, String, int)
     */
    public static String substring(final String text, String beginString) {
        return substring(text, beginString, 0);
    }

    /**
     * [?]:?(?)??,shift?????.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * StringUtil.substring("jinxin.feilong",".",0)     =   ".feilong"
     * StringUtil.substring("jinxin.feilong",".",1)     =   "feilong"
     * </pre>
     * 
     * </blockquote>
     *
     * @param text
     *            text
     * @param beginString
     *            beginString
     * @param shift
     *            ??,??,0??
     * @return  <code>text</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          <code>beginString</code> nullempty, {@link StringUtils#EMPTY}<br>
     *          text.indexOf(beginString)==-1, {@link StringUtils#EMPTY}<br>
     *         {@code  beginIndex + shift < 0}, {@link IllegalArgumentException}<br>
     *         {@code  beginIndex + shift > text.length()}, {@link StringUtils#EMPTY}<br>
     *         ? text.substring(beginIndex + shift)<br>
     * @see org.apache.commons.lang3.StringUtils#substringAfter(String, String)
     */
    public static String substring(final String text, String beginString, int shift) {
        if (isNullOrEmpty(text) || isNullOrEmpty(beginString)) {
            return EMPTY;
        }

        int beginIndex = text.indexOf(beginString);
        if (beginIndex == StringUtils.INDEX_NOT_FOUND) {// ?
            return EMPTY;
        }
        //****************************************************
        int startIndex = beginIndex + shift;
        Validate.isTrue(startIndex >= 0,
                Slf4jUtil.format("[{}] index[{}]+shift[{}]<0,text[{}]", beginString, beginIndex, shift, text));

        int textLength = text.length();
        if (startIndex > textLength) {
            LOGGER.warn("beginString [{}] index[{}]+shift[{}]>text[{}].length()[{}]", beginString, beginIndex,
                    shift, text, textLength);
            return EMPTY;
        }
        return text.substring(startIndex);// 0
    }

    /**
     * ???.
     * 
     * <pre class="code">
     * StringUtil.addDoubleQuotes("jinxin.feilong") = "jinxin.feilong"
     * </pre>
     * 
     * @param text
     *            ?
     * @return "\"" + text + "\""
     * @see "org.springframework.util.StringUtils#quote(String)"
     */
    public static String addDoubleQuotes(String text) {
        return "\"" + text + "\"";
    }

    /**
     * ?,(???).
     * 
     * <pre class="code">
     * StringUtil.stringAddInt("002",2)            =   004
     * StringUtil.stringAddInt("000002",1200)      =   001202
     * </pre>
     * 
     * @param str
     *            ?  002
     * @param i
     *            
     * @return ?,(???).
     * @see NumberUtil#toString(Number, String)
     */
    public static String stringAddInt(String str, int i) {
        String pattern = "";
        for (int j = 0, z = str.length(); j < z; ++j) {
            pattern += "0";
        }
        return NumberUtil.toString(Integer.parseInt(str) + i, pattern);
    }

    /**
     * String add int.
     */
    @Test
    public void stringAddInt() {
        assertEquals("004", stringAddInt("002", 2));
        assertEquals("001202", stringAddInt("000002", 1200));
    }

    /**
     * Adds the double quotes.
     */
    @Test
    public void addDoubleQuotes() {
        assertEquals("\"" + "jinxin.feilong" + "\"", addDoubleQuotes("jinxin.feilong"));
    }
}