blue-sky
blue-sky

Reputation: 53866

Extract a string within a string

How can I access WWW.GOOGLE.COM within this String.

I can use the substring method to chop off javascript:linkToExternalSite(' but how can I remove the subsequest string.

So that

javascript:linkToExternalSite('WWW.GOOGLE.COM','D','SDFSDX2131D','R','','X','D','DFA','SAFD')

becomes

WWW.GOOGLE.COM

This works

string = string.substring(31, string.length());
string = string.substring(0, string.indexOf("'"));

Upvotes: 0

Views: 95

Answers (4)

user1945649
user1945649

Reputation: 35

You Can use the substring() Method Of String Class!

Upvotes: 0

Sam Palmer
Sam Palmer

Reputation: 1725

Use regex

String substring = //YOURSUBSTRING
    Pattern p = Pattern.compile("[W]{3}[.A-Z]+[.COM|.CO.UK]", Pattern.CASE_INSENSITIVE);

    Matcher matcher = p.matcher(substring);

        if (matcher.find()){
            String Url = matcher.group();
            System.out.println(matcher.group());

        }

Upvotes: 2

Ingo
Ingo

Reputation: 36349

You don't need to, there is an easier way:

final String it = "WWW.GOOGLE.COM";

No need for chop, cut or other bloody operations.

Upvotes: 2

John B
John B

Reputation: 32969

If myString = "www.google.com','..."

You could do:

result = myString.substring(0, myString.indexof("'");

Upvotes: 2

Related Questions