public class Bracket { String left = null;
String right = null;
Bracket b = null;
public Bracket(String s) {
int i = s.indexOf('[');
int j = s.lastIndexOf(']');
if(i != -1 && j != -1)
{
b = new Bracket(s.substring(i + 1,j));
left = s.substring(0,i);
right = s.substring(j + 1);
}
else
{
left = s;
}
} public void output(int classNum)
{
if(b != null)
{
int i = 0, j=0;
i = left.indexOf('<');
while(i!=-1)
{
j = left.indexOf('>', i);
System.out.println("变量值=" + left.substring(i+1,j) + ",级别 = " + classNum);
i = left.indexOf('<', j);
} i = right.indexOf('<');
while(i!=-1)
{
j = right.indexOf('>', i);
System.out.println("变量值=" + right.substring(i + 1,j) + ",级别 = " + classNum);
i = right.indexOf('<', j);
}
b.output(classNum + 1);
}
else
{
int i = 0, j=0;
i = left.indexOf('<');
while(i!=-1)
{
j = left.indexOf(">", i);
System.out.println("变量值=" + left.substring(i + 1,j) + ",级别 = " + classNum);
i = left.indexOf("<", j);
}
}
}
public static void main(String[] args) {
String s = "你好<name>先生 本月你购买的商品为:[产品名:<product>,产品数量<productnum>] 购买日期:<date>";
Bracket Bracket1 = new Bracket(s);
Bracket1.output(1);
}
}
String right = null;
Bracket b = null;
public Bracket(String s) {
int i = s.indexOf('[');
int j = s.lastIndexOf(']');
if(i != -1 && j != -1)
{
b = new Bracket(s.substring(i + 1,j));
left = s.substring(0,i);
right = s.substring(j + 1);
}
else
{
left = s;
}
} public void output(int classNum)
{
if(b != null)
{
int i = 0, j=0;
i = left.indexOf('<');
while(i!=-1)
{
j = left.indexOf('>', i);
System.out.println("变量值=" + left.substring(i+1,j) + ",级别 = " + classNum);
i = left.indexOf('<', j);
} i = right.indexOf('<');
while(i!=-1)
{
j = right.indexOf('>', i);
System.out.println("变量值=" + right.substring(i + 1,j) + ",级别 = " + classNum);
i = right.indexOf('<', j);
}
b.output(classNum + 1);
}
else
{
int i = 0, j=0;
i = left.indexOf('<');
while(i!=-1)
{
j = left.indexOf(">", i);
System.out.println("变量值=" + left.substring(i + 1,j) + ",级别 = " + classNum);
i = left.indexOf("<", j);
}
}
}
public static void main(String[] args) {
String s = "你好<name>先生 本月你购买的商品为:[产品名:<product>,产品数量<productnum>] 购买日期:<date>";
Bracket Bracket1 = new Bracket(s);
Bracket1.output(1);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货