Как найти родительскую ссылку с неработающей связью с селеном? [Дубликат]

*

В swift 3.0

*

 if UIDevice.current.userInterfaceIdiom == .pad {
        //pad
    } else if UIDevice.current.userInterfaceIdiom == .phone {
        //phone
    } else if UIDevice.current.userInterfaceIdiom == .tv {
        //tv
    } else if UIDevice.current.userInterfaceIdiom == .carPlay {
        //CarDisplay
    } else {
        //unspecified
    }
11
задан Eonil 15 April 2012 в 03:59
поделиться

3 ответа

Самый короткий фрагмент кода, о котором я могу думать, это:

URI uri = new URI("http://www.stackoverflow.com/path/to/something");

URI parent = uri.getPath().endsWith("/") ? uri.resolve("..") : uri.resolve(".")
25
ответ дан ɲeuroburɳ 21 August 2018 в 16:08
поделиться
  • 1
    URI.resolve также отлично подходит для более сложных операций ... например, uri.resolve («dir / file.txt») – David 20 October 2013 в 20:25
  • 2
    Это, похоже, не работает для URI с протоколом jar: file: ... & quot ;, который встречается с ресурсами, которые находятся в файлах JAR. – Daniel Thommes 1 October 2016 в 19:52

Вот очень простое решение, которое было лучшим в моем случае:

private String getParent(String resourcePath) {
    int index = resourcePath.lastIndexOf('/');
    if (index > 0) {
        return resourcePath.substring(0, index);
    }
    return "/";
}

Я создал простую функцию, меня вдохновил код File::getParent. В моем коде нет проблем с обратными косыми чертами в Windows. Я предполагаю, что resourcePath является ресурсной частью URL, без протокола, домена и номера порта. (например, /articles/sport/atricle_nr_1234)

0
ответ дан Tomasz Szymulewski 21 August 2018 в 16:08
поделиться

Я не знаю библиотечной функции, чтобы сделать это за один шаг. Однако следующий (по общему признанию, громоздкий) бит кода, который, как я полагаю, выполняет то, что вам нужно (и вы можете обернуть это в свою собственную служебную функцию):

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest
{
    public static void main( String[] args ) throws MalformedURLException
    {
        // make a test url
        URL url = new URL( "http://stackoverflow.com/questions/10159186/how-to-get-parent-url-in-java" );

        // represent the path portion of the URL as a file
        File file = new File( url.getPath( ) );

        // get the parent of the file
        String parentPath = file.getParent( );

        // construct a new url with the parent path
        URL parentUrl = new URL( url.getProtocol( ), url.getHost( ), url.getPort( ), parentPath );

        System.out.println( "Child: " + url );
        System.out.println( "Parent: " + parentUrl );
    }
}
3
ответ дан ulmangt 21 August 2018 в 16:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: