用正则表达式:import java.util.regex.*;public class Regtest{ public static void main(String[] args){ String str = "<abc>fdsalfjorjlasdfsadfasfsadfewkrlq<b>wrf</b>lsdmcx</abc>"; Pattern p = Pattern.compile("<abc>([\\S]+)</abc>"); Matcher m = p.matcher(str); boolean result = m.find(); String s = m.group(1); System.out.println(s);
String类的indexof()或者lastIndexof()定位你的字符串的开始和结束,在用subString()以字符串的开始和结束位置为参数,得到子串。
最好是用正则。
如果不用正则的话,str.subString(str.indexOf("<abc>"),str.indexOf("</abc>")+"</abc>".length)得到结果
可以自己写一个函数,传入两个参数,例如"<abc>","</abc>".
public static void main(String[] args){
String str = "<abc>fdsalfjorjlasdfsadfasfsadfewkrlq<b>wrf</b>lsdmcx</abc>";
Pattern p = Pattern.compile("<abc>([\\S]+)</abc>");
Matcher m = p.matcher(str);
boolean result = m.find();
String s = m.group(1);
System.out.println(s);
}
}