第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何分割 URL 并僅獲取主機名

如何分割 URL 并僅獲取主機名

繁星淼淼 2024-01-25 10:46:58
例如,我正在嘗試拆分 URL,https://stackoverflow.com/questions/并僅使用stackoverflow.com. 在不使用內(nèi)置函數(shù)的情況下,如何在 Java 中執(zhí)行此操作getHost()?
查看完整描述

3 回答

?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

干得好 :


    Pattern pattern = Pattern.compile("^(?:(?:http)s?://)?(?<hostGroup>[^/:]+).*");

    Matcher matcher = pattern.matcher("https://stackoverflow.com/questions/");


    if (matcher.matches()) {

        System.out.println(matcher.group("hostGroup"));

    } else {

        System.out.println("Not found! Invalid URL ^^");

    }

上面將找到stackoverflow.com的以下 url 字符串:

  • https://stackoverflow.com/questions/

  • http://stackoverflow.com/questions/

  • stackoverflow.com/questions/

  • stackoverflow.com:80/questions/

  • stackoverflow.com/

  • stackoverflow.com

我想那是為了練習正則表達式?否則,盡可能使用標準 API -(根據(jù)您的情況URI#getHost()?。?/p>

干杯!


查看完整回答
反對 回復 2024-01-25
?
叮當貓咪

TA貢獻1776條經(jīng)驗 獲得超12個贊

如果您可以將 URL 放入字符串中,則有以下選項:


public static void main(String []args){

  String str ="https://stackoverflow.com/questions/";

  String[] parts = str.split("/");

  String part1 = parts[0]; //https:

  String part2 = parts[1]; //'nothing'

  String part3 = parts[2]; //stackoverflow.com

  String part4 = parts[3]; //questions

}


查看完整回答
反對 回復 2024-01-25
  • 3 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號