String str = [折扣] > 10 and [优惠] > 20 怎么取出所有[]中的所有数据啊,就是取得折扣、优惠 String str = [折扣] > 10 and [优惠] > 20 怎么取出所有[]中的所有数据啊,就是取得折扣、优惠 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.ArrayList;public class TestA { public static void main(String[] args) { ArrayList<String> s = TestA.getString("[折扣] > 10 and [优惠] > 20 ", "[", "]"); for(int i=0;i<s.size();i++){ //System.out.println(s.get(i)); } } public static ArrayList<String> getString(String str, String start, String end) { ArrayList<String> lst = new ArrayList<String>(); while (true) { int i = str.indexOf(start); int j = str.indexOf(end); if (i > -1 && j > -1 && i < j) { if (!str.substring(i+1, j).equals("")) { lst.add(str.substring(i+1, j)); System.out.println(str.substring(i+1, j)); } str = str.substring(j); } else if (i > -1 && j > -1 && i > j) { str = str.substring(i); } else { break; } } return lst; }} import java.util.regex.Matcher;import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String str = "[折扣] > 10 and [优惠] > 20 "; Pattern p = Pattern.compile("\\[.*?\\]"); Matcher m = p.matcher(str); while(m.find()) { System.out.println(m.group()); } } }输出结果:[折扣][优惠] 路往哪儿走 对于Math.round(-11.5)的理解 多线程很难懂呀,大家有没有好学习方法? jacob操作word的问题 对对象和引用的一点疑问,求教 把数据结构掌握好重要还是把精力发在JAVA语言上??? 如何将java程序在 windows2000 Server 上在后台进行运行! if 看完 Thinging in Java 能做什么? 请问J2SETM分为的:JRE和SDK,它们有什么不同????????? 请问一下在Apache+tomcat下根目录在哪里? 在JList中增加滚动条 有关类关联的问题
public static void main(String[] args) {
ArrayList<String> s = TestA.getString("[折扣] > 10 and [优惠] > 20 ", "[", "]");
for(int i=0;i<s.size();i++){
//System.out.println(s.get(i));
}
} public static ArrayList<String> getString(String str, String start,
String end) {
ArrayList<String> lst = new ArrayList<String>(); while (true) {
int i = str.indexOf(start);
int j = str.indexOf(end);
if (i > -1 && j > -1 && i < j) {
if (!str.substring(i+1, j).equals("")) {
lst.add(str.substring(i+1, j));
System.out.println(str.substring(i+1, j));
}
str = str.substring(j);
} else if (i > -1 && j > -1 && i > j) {
str = str.substring(i);
} else {
break;
}
}
return lst;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "[折扣] > 10 and [优惠] > 20 ";
Pattern p = Pattern.compile("\\[.*?\\]");
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println(m.group());
}
}
}输出结果:
[折扣]
[优惠]