2013年9月13日星期五

A string parameter substitution method ! Help

A string of characters to the format: Chinese + $ { 1} + Chinese or $ {N} For example: I play $ $ { 1} {2} how will he replace me and # p_1 # play # p_2 #

$ {N} for the parameters may not have more than one digital size of incremental other words: I play $ $ { 2} {1 } : Or: I play $ $ { 1} {3 } this format is possible with the guidance of God seeking large JAVA
------ Solution ----------------------- ---------------------
	public static void main(String[] args) {
String str = "我和${1}去打${2} ";
str = str.replaceAll("\\$\\{(\\d+)\\}", "#p_$1#");
System.out.println(str);
}

------ Solution ------------- -------------------------------

		public static void main(String[] args) {
String str = "我和#p_1#去打#p_2#";
str = str.replaceAll("#p_(\\d+)#", "\\$\\{$1\\}");
System.out.println(str);
}

------ For reference only ----------------------------------- ----
ashamed ah ! You suddenly I solved this still thinking how to replace the loop ! Hey ! If they have to replace Le example will turn me and me and # p_1 # play # p_2 #: Replace : I play $ $ { 1} {2 } < br>

没有评论:

发表评论