Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查子字符串是否包含括号,如果包含括号,如何在Java中删除整个子字符串?_Java_String_Substring - Fatal编程技术网

如何检查子字符串是否包含括号,如果包含括号,如何在Java中删除整个子字符串?

如何检查子字符串是否包含括号,如果包含括号,如何在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

正在从JTextPane获取输入。到目前为止,我的代码只是隔离了子字符串。Java中有没有一个方法可以用来删除它

例如,此处的字符串:

此字符串中有七(7)个单词

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)?