0

重複の可能性:
特定の URL からパラメーターを抽出する方法

この URL のパラメータから数字だけを取得しようとしています:

htt://tesing12/testds/fdsa?communityUuid=45352-32452-52

私は運がないのでこれを試しました:

^.*communityUuid=

どんな助けでもいいでしょう。

4

2 に答える 2

4

単純な文字列操作ルートはお勧めしません。より冗長でエラーが発生しやすくなります。組み込みのクラスから少し助けを得て、URL(「&」で区切られたパラメーター)を使用しているという知識を使用して、実装をガイドすることもできます。

String queryString = new URL("http://tesing12/testds/fdsa?communityUuid=45352-32452-52").getQuery();

String[] params = queryString.split("&");

String communityUuid = null;
for (String param : params) {
    if (param.startsWith("communityUuid=")) {
        communityUuid = param.substring(param.indexOf('=') + 1);
    }
}

if (communityUuid != null) {
    // do what you gotta do
}

これにより、URLの整形式性をチェックできるという利点があり、同様の名前のパラメーターから発生する可能性のある問題を回避できます(文字列操作ルートは、「abc_communityUuid」と「communityUuid」の値を報告します)。

このコードの便利な拡張機能は、「params」を反復処理しながらマップを作成し、必要なパラメーター名をマップに照会することです。

于 2012-12-03T20:52:31.657 に答える
3

正規表現を使用する理由がわかりません。

私はこれを行うだけです:

String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int index = url.indexOf(token) + token.length();
String theNumbers = url.substring(index);

ノート:

次のパラメーターも探す必要がある場合があります。

String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int startIndex = url.indexOf(token) + token.length();
// here's where you might want to use a regex
String theNumbers = url.substring(startIndex).replaceAll("&.*$", "");
于 2012-12-03T20:28:30.763 に答える