该方法用于实现终端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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。