該方法用於實現終端append-and-replace 步驟。此方法從輸入序列中讀取字符,從替換的追加位置開始,並將它們追加到給定的字符串緩衝區。
用法
public StringBuffer appendTail(StringBuffer strbuff)
參數
strbuff- 目標字符串緩衝區
返回
目標字符串緩衝區
例子1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherAppendTailExample1 {
public static void main(String[] args) {
//pattern/expression to be matched
Pattern p = Pattern.compile("a+");
//Regular expression
Matcher m = p.matcher(" 13aaa08aaa2018 ");
// Creating the target string buffer
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "-");//calling method
}
// Appending the string buffer to the end
m.appendTail(sb);
//converting String Buffer to String
System.out.println(sb.toString());
}
}
輸出:
13-08-2018
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherAppendTailExample2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//pattern/expression to be matched
Pattern p = Pattern.compile("a+");
//Regular expression
Matcher m = p.matcher(" 13aaa08aaa2018 ");
// Creating the target string buffer
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb,"-" );
System.out.println(" start:"+m.start()+", End:"+m.end());
}
// Appending the string buffer to the end
m.appendTail(sb);
//converting String Buffer to String
System.out.println("After Replacing Matching position:"+sb.toString());
}
}
上述程序的輸出
start:3, End:6 start:8, End:11 After Replacing Matching position:13-08-2018
相關用法
- Java Matcher appendTail(StringBuffer)用法及代碼示例
- Java Matcher appendTail(StringBuilder)用法及代碼示例
- Java Matcher appendReplacement(StringBuilder, String)用法及代碼示例
- Java Matcher appendReplacement(StringBuffer, String)用法及代碼示例
- Java Matcher appendReplacement()用法及代碼示例
- Java Matcher region()用法及代碼示例
- Java Matcher matches()用法及代碼示例
- Java Matcher replaceAll(String)用法及代碼示例
- Java Matcher group(String)用法及代碼示例
- Java Matcher start(int)用法及代碼示例
- Java Matcher replaceFirst(String)用法及代碼示例
- Java Matcher useTransparentBounds()用法及代碼示例
- Java Matcher lookingAt()用法及代碼示例
- Java Matcher quoteReplacement()用法及代碼示例
- Java Matcher end()用法及代碼示例
- Java Matcher useAnchoringBounds()用法及代碼示例
- Java Matcher hasAnchoringBounds()用法及代碼示例
- Java Matcher toMatchResult()用法及代碼示例
- Java Matcher replaceAll(Function)用法及代碼示例
- Java Matcher requireEnd()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Matcher appendTail() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。