import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.text.StrSubstitutor;
import bsh.Interpreter;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
Map map = new HashMap();
map.put("N1", new Integer(1));
map.put("N2", new Integer(2));
map.put("N3", new Integer(3));
map.put("N4", new Integer(4));
map.put("N5", new Integer(22));
map.put("N6", new Integer(6));
String str = "你好$${${N5}+${N6}-${N4}}";
StrSubstitutor strSubstitutor = new StrSubstitutor(map);
String result = t.operateVariable(strSubstitutor.replace(str),map);
System.out.print(result);
}
public String operateVariable(String str ,Map map){
String result = "";
int begin = 0;
int end = 0;
int count = 0;
String temp = "";
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='$'){
begin = i+2;
result += str.substring(end, i);
}
if(str.charAt(i)=='}'){
end = i+1;
temp = str.substring(begin, i);
Interpreter ii = new Interpreter();
try{
count = ((Integer)ii.eval(temp)).intValue();
}catch(Exception e){
e.printStackTrace();
}
result += count;
}
if(i==str.length()-1){
result += str.substring(end, i+1);
}
}
return result;
}
}
import java.util.Map;
import org.apache.commons.lang.text.StrSubstitutor;
import bsh.Interpreter;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
Map map = new HashMap();
map.put("N1", new Integer(1));
map.put("N2", new Integer(2));
map.put("N3", new Integer(3));
map.put("N4", new Integer(4));
map.put("N5", new Integer(22));
map.put("N6", new Integer(6));
String str = "你好$${${N5}+${N6}-${N4}}";
StrSubstitutor strSubstitutor = new StrSubstitutor(map);
String result = t.operateVariable(strSubstitutor.replace(str),map);
System.out.print(result);
}
public String operateVariable(String str ,Map map){
String result = "";
int begin = 0;
int end = 0;
int count = 0;
String temp = "";
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='$'){
begin = i+2;
result += str.substring(end, i);
}
if(str.charAt(i)=='}'){
end = i+1;
temp = str.substring(begin, i);
Interpreter ii = new Interpreter();
try{
count = ((Integer)ii.eval(temp)).intValue();
}catch(Exception e){
e.printStackTrace();
}
result += count;
}
if(i==str.length()-1){
result += str.substring(end, i+1);
}
}
return result;
}
}