2

For example, I have a string as below:

<http://www.w3.org/2000/01/rdf-schema#label> "Telecommunications law"@en <http://en.wikipedia.org/wiki/> 

What is the easiest way to extract the substring:

Telecommunication law

Please note that the substring contains a space.

1

5 Answers 5

3

You could use Pattern and Matcher :

Pattern p = Pattern.compile("\".*\"");
Matcher m = p.matcher(s);

if(m.find()){
   String resultString = m.group();
}

In your case, resultString will contain ["Telecommunications law"] and you can trim the double quotes if you don't want to keep them.

Sign up to request clarification or add additional context in comments.

Comments

1

String.split() the string on ", and select the second element in the returned array:

String tokens[] = yourString.split("\"");

// tokens[1] will contain Telecommunications law

1 Comment

I want extract substring by using regex.
0

what do you mean "extracting the string" ?

getting the first occurence of the string is by:

int index = string.indexOf("Telecommunications law");

the most efficient way of getting what is in between the first parenthesis and the second is by:

final String test="http://www.w3.org/2000/01/rdf-schema#label \"Telecommunications law\"@en http://en.wikipedia.org/wiki/";
final int firstIndex=test.indexOf('\"');
final int lastIndex=test.indexOf('\"',firstIndex+1);
final String result=test.substring(firstIndex+1,lastIndex);
System.out.println(result);

4 Comments

@andriod developer from that string i need only Telecommuniation law.
again , do you want what is in between the parenthesis ? or maybe you just want to check if the specified string exists? either way , i've answered both using the code i've written .
@andriod develope you wright in 2nd question(want to check if the specified string exists) and want what is between the parenthesis.
i don't understand . if you mean how to check if the string exists , just check that the integer you get from the first code is non-negative.
0
    public static void main(String[] args) {
 String str = "http://www.w3.org/2000/01/rdf-schema#label \"Telecommunications law\"@en http://en.wikipedia.org/wiki/" ;

 String temp = str.substring(str.indexOf('\"')+1, str.indexOf('\"',str.indexOf('\"')+1));
 System.out.print(temp);

    }

Comments

0
public static void main(String args[]){
String yourString = "<http://www.w3.org/2000/01/rdf-schema#label> \"Telecommunications law\"@en <http://en.wikipedia.org/wiki/>";
        String tokens[] = yourString.split("\"");

        for(int i = 0; i < tokens.length; i++){
            if(tokens[i].equals("Telecommunications law")){
                System.out.println(tokens[i]);
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.