当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Matcher appendTail()用法及代码示例


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