字符串截取问题 方法返回的字符串是这样的 XXXX[YYYY]其中X,Y是动态的 不确定是多少字符 请问 怎么把[]中的Y内容取出来 不要[]! 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int start=string.indexof('[');这个方法可以找到[的下标然后从start这个下标开始到结尾的前一个坐标结束str=string.substring(start+1,string.length()-1); public class Test { public static void main(String[] args) { String str = "xxx[yyy]"; //"["所在位置 int beginIndex = str.indexOf("["); //"]"所在位置 int endIndex = str.indexOf("]"); //如果"["和"]"存在 if(beginIndex!=-1 && endIndex!=-1){ String endString = str.substring(beginIndex+1, endIndex); System.out.println(endString); } }} public static void main(String args[]) { String str="XXXX[YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY]"; Pattern p=Pattern.compile("\\[(\\w+)\\]"); Matcher m=p.matcher(str); while(m.find()) System.out.println(m.group(1));} 二楼正解,还有就是用lastIndexof("[");lastIndexof("]"),也ok public static void main(String[] args) { String s = "XXXX[YY啊啊啊啊#@#$%234234^&*((^% YY]"; System.out.println(s.replaceAll("(^.+\\[(.+?)\\]$)", "$2")); }输出:YY啊啊啊啊#@#$%234234^&*((^% YY 多看看javaAPI...这些简单的问题就不要再发过来了... java图书管理系统 乱码问题 自学java从何学起 设计一个类,让它只能实例化3次,怎么实现啊? 弱弱问个数组排序问题 做哪个程序员? 急!java1.2不能正常运行,XP SP1上能正常运行吗? servlet怎么调试,就像单步执行一样? 怎么触发鼠标右键事件 请教jBuilder5中如何把一个JAVA文件变为可执行文件! String a = null;和String a=“”哪种好? java线程问题
然后从start这个下标开始到结尾的前一个坐标结束
str=string.substring(start+1,string.length()-1);
public class Test {
public static void main(String[] args) {
String str = "xxx[yyy]";
//"["所在位置
int beginIndex = str.indexOf("[");
//"]"所在位置
int endIndex = str.indexOf("]");
//如果"["和"]"存在
if(beginIndex!=-1 && endIndex!=-1){
String endString = str.substring(beginIndex+1, endIndex);
System.out.println(endString);
}
}
}
public static void main(String args[]) {
String str="XXXX[YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY]";
Pattern p=Pattern.compile("\\[(\\w+)\\]");
Matcher m=p.matcher(str);
while(m.find())
System.out.println(m.group(1));
}
String s = "XXXX[YY啊啊啊啊#@#$%234234^&*((^% YY]";
System.out.println(s.replaceAll("(^.+\\[(.+?)\\]$)", "$2"));
}
输出:
YY啊啊啊啊#@#$%234234^&*((^% YY