import java.util.ArrayList;
import java.util.List;
public class test7 {
/**
* 根据特定字符获取特定字符内的字符串
* @param _str 原字符串
* @param _bjc 特定字符串标记例如:(),{},[],【】,《》等
* @param _snum 需要截取的第几个特定字符的内容,从左往右数,例如:(1+2+(2+3)),取第二个得到的就是2+3
* @return
*/
public static String getSpecificContentByString(String _str,String _bjc,int _snum){
String reStr=null;
char[] _cStr=_bjc.toCharArray();
char _leftc=_cStr[0];
char _rightc=_cStr[1];
String subAftStr=_str;
if(_str.contains(String.valueOf(_leftc))&&
_str.contains(String.valueOf(_rightc))){
int _leftNum= (int) (_str.chars().filter(c -> c == _leftc).count());
int _rightNum= (int) (_str.chars().filter(c -> c == _rightc).count());
if(_leftNum<_snum||_rightNum<_snum) {
throw new RuntimeException("需要获取的字符串不存在");
}
List<Integer> _lcIndexList=new ArrayList<Integer>();
int[] _lcIndex=new int[_leftNum];
int[] _rcIndex=new int[_rightNum];
char[] _charStr=_str.toCharArray();
int _indexLeftNum=0;
for(int index=0;index<_charStr.length;index++) {
char _cs=_charStr[index];
if(_cs==_leftc) {
_lcIndex[_indexLeftNum]=index;
_lcIndexList.add(index);
if(_lcIndexList.size()==1) {
if(subAftStr.substring(index+1,
_str.indexOf(String.valueOf(_rightc))).
contains(String.valueOf(_leftc))) {
_rcIndex[_indexLeftNum]=_str.lastIndexOf(String.valueOf(_rightc))-1;
}else {
_rcIndex[_indexLeftNum]=_str.indexOf(String.valueOf(_rightc));
}
}else {
boolean _loopFlag=true;
int _nextSubRiNum=index+1;
boolean sfpd=true;
while(_loopFlag) {
String _khSubStr=_str.substring(_nextSubRiNum);
int _subRiNum=_khSubStr.indexOf(String.valueOf(_rightc))+_nextSubRiNum;
_khSubStr=_khSubStr.substring(0, _khSubStr.indexOf(String.valueOf(_rightc)));
if(!_khSubStr.contains(String.valueOf(_leftc))&&sfpd) {
if(_str.charAt(_subRiNum)==_rightc) {
_rcIndex[_indexLeftNum]=_subRiNum;
}else {
_rcIndex[_indexLeftNum]=_subRiNum-1;
}
break;
}else {
sfpd=false;
_nextSubRiNum=_str.substring(_subRiNum).
indexOf(String.valueOf(_rightc))+_subRiNum+1;
_khSubStr=_str.substring(index,_nextSubRiNum);
int _subLeftNum= (int) (_khSubStr.chars().filter(c -> c
== _leftc).count());
int _subRightNum= (int) (_khSubStr.chars().filter(c -> c
== _rightc).count());
if(_subLeftNum==_subRightNum) {
_rcIndex[_indexLeftNum]=_nextSubRiNum-2;
break;
}
}
}
}
_indexLeftNum++;
}
}
if(_leftNum!=_rightNum) {
throw new RuntimeException("字符串格式错误");
}
int _lkhindex=_lcIndex[_snum-1];
int _rkhindex=_rcIndex[_snum-1];
subAftStr=subAftStr.substring(_lkhindex+1, _rkhindex+1);
if(subAftStr.contains(String.valueOf(_leftc))&&
subAftStr.indexOf(_rightc)<subAftStr.indexOf(_leftc)) {
subAftStr=subAftStr.substring(0, subAftStr.indexOf(_rightc));
}
if(!subAftStr.contains(String.valueOf(_leftc))
&&subAftStr.contains(String.valueOf(_rightc))) {
subAftStr=subAftStr.substring(0, subAftStr.indexOf(_rightc));
}
String _subString=subAftStr;
reStr=_subString;
}
return reStr;
}
public static void main(String[] args) {
String ss="x^2+y^3+c^4+(z^3+y^2+(x*y)+(z+y)+((((z+x)*2)*3)+3))^2";
System.out.println("i1:"+getSpecificContentByString(ss, "()", 1));
System.out.println("i2:"+getSpecificContentByString(ss, "()", 2));
System.out.println("i3:"+getSpecificContentByString(ss, "()", 3));
System.out.println("i4:"+getSpecificContentByString(ss, "()", 4));
System.out.println("i5:"+getSpecificContentByString(ss, "()", 5));
System.out.println("i6:"+getSpecificContentByString(ss, "()", 6));
System.out.println("i6:"+getSpecificContentByString(ss, "()", 7));
String ss1="x^2+y^3+c^4+(z^3+y^2+{x*y}+(z+y)+{(((z+x)*2)*3)+3})^2";
System.out.println("d1:"+getSpecificContentByString(ss1, "{}", 1));
System.out.println("d2:"+getSpecificContentByString(ss1, "{}", 2));
System.out.println("d3:"+getSpecificContentByString(ss1, "()", 3));
}
结果
不仅仅是大小括号包括然后不同的两个字符都行