比较两个字符串,例如android中的url

时间:2018-09-05 19:23:06

标签: java android kotlin

我有两个字符串:

http://porter.com/request/.*

http://porter.com/request/tokenId

我想检查第一部分:http://porter.com/request是否相同,并检查tokenId是否不为空,因为在某些情况下,它可能仅为http://porter.com/request/。 我使用类似的东西:

override fun validate(pair: Pair<URI, URI>): Boolean {
        val uri = pair.first.path.split("/").dropLast(1).filter { it.isNotBlank() }.joinToString("")
        val uriIntent = pair.second.path.split("/").dropLast(1).filter { it.isNotBlank() }.joinToString("")

        val asd = pair.second.path.split("/").filter { it.isNotBlank() }.last().isNotBlank()

        return uri == uriIntent && asd
    }

,但这不适用于以下情况:http://porter.com/request/ 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

final String regex = "(http://porter.com/request/).+";

/**
 * Below code will return false
 * since, URL doesn't have last path
 */
final String yourUrl = "http://porter.com/request/.*";
final boolean valid = yourUrl.matches(regex)

/**
 * Same (will return false), as ex. above
 */

final String yourUrl = "http://porter.com/request/*";
final boolean valid = yourUrl.matches(regex)

/**
 * This will return true. Link is Ok.
 */

final String yourUrl = "http://porter.com/request/tokenId";
final boolean valid = yourUrl.matches(regex)