當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Matcher quoteReplacement()用法及代碼示例


Matcher 類的 quoteReplacement() 方法返回指定字符串的文字替換字符串。該方法可以和matcher對象一起使用,在matcher類的appendReplacement方法中進行替換操作。

用法

public static String quoteReplacement(String s)

參數

s- 要文字化的字符串

返回

文字字符串替換

自從

1.5

例子1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherquoteReplacementExample1{
			private static String INPUT ;
		   public static void main(String[] args) {
		     //Pattern creation
		     Pattern pattern = Pattern.compile("Independent");
		     // creating matcher object
		     Matcher matcher = pattern.matcher("java is platform Independent "); 
		      INPUT = matcher.replaceAll(matcher.quoteReplacement(":)"));
		      System.out.println(INPUT);
		   }		
	}

輸出:

java is platform:) 

例子2

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherquoteReplacementExample2 {
				private static String input ;	
		      public static void main(String[] args) {
		         //Pattern creation
		    	  Pattern p= Pattern.compile("javaprogramming");
		         //creating matcher object
Matcher m = p.matcher("javaprogramming is secure " + ".javaprogramming is platform"); 
		         try{
		            //Below line will throw exception
		        	 input = m.replaceAll("java$");
		         } catch(Exception e){
		            System.out.println("Exception:"+ e.getMessage());
		         }
		         input = m.replaceAll(m.quoteReplacement("java$"));
		         System.out.println(input);
		  }
}

輸出:

Exception:Illegal group reference:group index is missing
java$ is secure .java$ is platform




相關用法


注:本文由純淨天空篩選整理自 Java Matcher quoteReplacement() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。