0
0

java。urlからドメインとドメイン以降の別に取り出す

Posted at
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static void main(String[] args) {
        String urlString = "https://www.example.com/path/to/resource";
        
        try {
            URL url = new URL(urlString);
            
            // ドメインを取得する
            String domain = url.getHost();
            System.out.println("ドメイン: " + domain);
            
            // ドメイン以降の部分を取得する
            String path = url.getPath();
            System.out.println("ドメイン以降の部分: " + path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

ドメイン: www.example.com
ドメイン以降の部分: /path/to/resource
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0