如何检查子字符串是否包含括号,如果包含括号,如何在Java中删除整个子字符串?
正在从JTextPane获取输入。到目前为止,我的代码只是隔离了子字符串。Java中有没有一个方法可以用来删除它 例如,此处的字符串: 此字符串中有七(7)个单词如何检查子字符串是否包含括号,如果包含括号,如何在Java中删除整个子字符串?,java,string,substring,Java,String,Substring,正在从JTextPane获取输入。到目前为止,我的代码只是隔离了子字符串。Java中有没有一个方法可以用来删除它 例如,此处的字符串: 此字符串中有七(7)个单词 public void insertString (int offset, String str, AttributeSet a) throws BadLocationException { super.insertString(offset, str, a); String text = getTe
public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
super.insertString(offset, str, a);
String text = getText(0, getLength());
int before = findLastNonWordChar(text, offset);
if (before < 0)
before = 0;
int after = findFirstNonWordChar(text, offset + str.length());
int wordL = before;
int wordR = before;
while(wordR <= after) {
if(wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
if(text.substring(wordL, wordR).contains("(\\W)*(())")){
//do something here to remove substring
}
wordL = wordR;
}
wordR++;
}
}
将生成以下字符串:
这个字符串中有单词
public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
super.insertString(offset, str, a);
String text = getText(0, getLength());
int before = findLastNonWordChar(text, offset);
if (before < 0)
before = 0;
int after = findFirstNonWordChar(text, offset + str.length());
int wordL = before;
int wordR = before;
while(wordR <= after) {
if(wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
if(text.substring(wordL, wordR).contains("(\\W)*(())")){
//do something here to remove substring
}
wordL = wordR;
}
wordR++;
}
}
public void insertString(int offset、String str、AttributeSet a)引发BadLocationException{
super.insertString(offset,str,a);
String text=getText(0,getLength());
int before=findLastNonWordChar(文本,偏移量);
如果(在<0之前)
前=0;
int after=findFirstNonWordChar(text,offset+str.length());
int-wordL=before;
int-wordR=before;
而(wordR我认为,您可以使用stringreplace方法删除该子字符串
if(text.substring(wordL, wordR).contains("(\\W)*(())")){
//do something here to remove substring
String newText = text.replace(text.substring(wordL, wordR), "");
}
是否要删除一对()之间的任何内容?是否有几对?输入是否仅包含9或仅包含9)?