Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, February 6, 2016

Getting substring from a given string in Java

Getting substring from a given string in Java


I am reading the content from a web page and then I am parsing it with the help of Jsoup parser to get only the hyperlinks that exists in the body section. I am getting the output as:

Sports  Titanic  license plates  miracle cars  Clear    and even more hyperlinks.  

From all of them, all I am interested in is data like

/sports/sports.asp  /titanic/titanic.asp  gastheft.asp  miracle.asp  /crime/warnings/clear.asp  

How can I do this using Strings or is there any other way or method to extract this information usinf Jsoup Parser itself?

Answer by Scary Wombat for Getting substring from a given string in Java


This should be a basic bit of parsign using

String.indexOf   

as in

index = jsoupOutput.indexOf ("href=\"");  

and

nextIndex = jsoupOutput.indexOf ("\"", index);  

with the necessary checks in place.

Answer by Mohammed R. El-Khoudary for Getting substring from a given string in Java


Let's assume that String anchor contains one of these links then the beginning index of the substring will after href=" and the end index will be the first quotation mark after index 9 this way:

String anchor = "Sports";  int beginIndex = anchor.indexOf("href=\"") + 6; //To start after 

And that's it if the shape of the anchor is going to always be that way.. better options are using regular expressions and best would be using an XML parser.

Answer by Abhijeet Kushe for Getting substring from a given string in Java


Use this as reference

import java.util.regex.*;    public class HelloWorld{         public static void main(String []args){             String s = "Sports"+                      "Titanic"+                      "license plates"+                      "miracle cars"+                      "Clear";         Pattern p = Pattern.compile("href=\".+?\"");         Matcher m = p.matcher(s);         while(m.find())         {             System.out.println(m.group().split("=")[1].replace("\"",""));         }         }  }  

Output

/sports/sports.asp  /titanic/titanic.asp  gastheft.asp  miracle.asp  /crime/warnings/clear.asp  

Answer by Engineer for Getting substring from a given string in Java


Try this it may help

String html = "

An example link.

"; Document doc = Jsoup.parse(html); Element link = doc.select("a").first(); String text = doc.body().text(); // "An example link" String linkHref = link.attr("href"); // "http://example.com/" String nextIndex = linkHref .indexOf ("\"", linkHref );

Answer by Shahinoor Shahin for Getting substring from a given string in Java


You can try this, its works.

public class AttributeParsing {    /**   * @param args   */  public static void main(String[] args) {      final String html = "Sports";        Document doc = Jsoup.parse(html, "", Parser.xmlParser());      Element th = doc.select("a[href]").first();        String href = th.attr("href");        System.out.println(th);      System.out.println(href);  }  

}

Output :

th : Sports

href : /sports/sports.asp

Answer by Bohemian for Getting substring from a given string in Java


You can do it in one line:

String[] paths = str.replaceAll("(?m)^.*?\"(.*?)\".*?$", "$1").split("(?ms)$.*?^");  

The first method call removes everything except the target from each line, and the second splits on newlines (will work on all OS terminators).

FYI (?m) turns on "multiline mode" and (?ms) also turns on the "dotall" flag.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.