Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:40+00:00 2026-05-27T03:52:40+00:00

NOTE : I am asking for real world problem, not for theoretical purpose; see

  • 0

NOTE: I am asking for real world problem, not for theoretical purpose; see the last part of the question — the same way browsers do the job.


Usually you would see the answer:

new java.net.URL(new java.net.URL(base_url),rel_url).toString

(base_url and rel_url are String). In my case base_url is the URL of page I fetched, rel_url comes from “<a href=…” value, so it might be even single “#” character (for example).

However such code does not work for URL fragments, like such two pieces:

htpp://www.hello.com/1.html

?p=2

I tested Firefox, Chromium, Opera, Konqueror, “Web Browser” (Gnome modesty ;-D) — all of them combine those URLs as:

htpp://www.hello.com/1.html?p=2

With code as above I get:

htpp://www.hello.com/?p=2

Question

How do you combine URL fragments, in a ready for world manner?

I hope there is already handy library for that, before I start doing parsing by myself ;-).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:52:41+00:00Added an answer on May 27, 2026 at 3:52 am

    You are misunderstanding what a URL is. ?p=2 is a query string, not a relative URL. (You may also find #foo, which is usually called a fragment identifier or reference and is most commonly used to jump to a section of a long document). The full scheme for URIs is described on Wikipedia among many other places (you can also find the differences between URIs and URLs in various places).

    Anyway, relative URLs refer only to the path part of the URL–it is whether the path is absolute or relative. If you have a query string and wish to attach it to an existing URL (which does not have a query string), just append it to the string. If you don’t know whether you have a query string, you can use the methods in the URL class to test for it.

    If you want to replicate what browsers do, given a full URL url and a String s,

    if (s.startsWith("?") || s.startsWith("#")) new java.net.URL(url.toString + s)
    else new java.net.URL(url, s)
    

    should do the trick. (I don’t know the exact code that different browsers use, but this replicates the behavior that you describe of appending a query string if that is all that is provided in a href.) If you don’t know whether your existing URLs might have query strings or not, then you can

    if (s.startsWith("#")) new java.net.URL(url.toString.takeWhile(_ != '#') + s)
    else if (s.startsWith("?")) new java.net.URL(url.toString.takeWhile(_ != '?') + s)
    else new java.net.URL(url, s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that this is asking a question about constructors, not about classes which
Note that I am not asking which to choose (MVC or MVP), but rather
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note : The code in this question is part of deSleeper if you want
Note This is not a REBOL-specific question. You can answer it in any language.
Please note: I am asking the question I want answered. I know this question
Please note that I am asking about the DESKTOP icon, not the window icon.
Note : Before asking this question I did an exhaustive search, and found little
(note: I'm usually more thorough in doing my research before asking a question here,
NOTE: I have updated this since originally asking the question to reflect some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.