String替換占位符


    /**
     * 依次替換占位符
     * 例如: 姓名:{s},電話:{s},郵箱:{s}  --> 姓名:小張,電話:18800000001,郵箱:abc@123.com
     * pattern = "\\{s}";
     *
     * @param input
     * @param pattern
     * @param texts
     * @param nullStr 不能為null
     * @return
     */
    public static String appendReplacement(String input, String pattern, String[] texts, String nullStr) {
        // 從正則表達式實例中運行方法並查看其如何運行
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        StringBuffer sb = new StringBuffer();
        int i = 0;
        while (m.find()) {
            // 將匹配之前的字符串復制到sb,再將匹配結果替換掉,並追加到sb
            m.appendReplacement(sb, i < texts.length && texts[i] != null ? texts[i] : nullStr);
            i++;
        }
        m.appendTail(sb);
        return sb.toString();
    }
Implements a non-terminal append-and-replace step. 

This method performs the following actions: 

1. It reads characters from the input sequence, starting at theappend position, and appends them to the given string buffer. Itstops after reading the last character preceding the previous match,that is, the character at index start() - 1. 


2. It appends the given replacement string to the string buffer. 


3. It sets the append position of this matcher to the index ofthe last character matched, plus one, that is, to end(). 


The replacement string may contain references to subsequencescaptured during the previous match: Each occurrence of ${name} or $gwill be replaced by the result of evaluating the corresponding group(name) or group(g)respectively. For $g,the first number after the $ is always treated as part ofthe group reference. Subsequent numbers are incorporated into g ifthey would form a legal group reference. Only the numerals '0'through '9' are considered as potential components of the groupreference. If the second group matched the string "foo", forexample, then passing the replacement string "$2bar" wouldcause "foobar" to be appended to the string buffer. A dollarsign ($) may be included as a literal in the replacementstring by preceding it with a backslash (\$). 

Note that backslashes (\) and dollar signs ($) inthe replacement string may cause the results to be different than if itwere being treated as a literal replacement string. Dollar signs may betreated as references to captured subsequences as described above, andbackslashes are used to escape literal characters in the replacementstring. 

This method is intended to be used in a loop together with the appendTail and find methods. Thefollowing code, for example, writes one dog two dogs in theyard to the standard-output stream: 

 Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM