gpt4 book ai didi

java - 在android中比较两个字符串如url

转载 作者:太空狗 更新时间:2023-10-29 13:03:30 25 4
gpt4 key购买 nike

我有两个字符串:

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

http://porter.com/request/tokenId

我想检查第一部分:http://porter.com/request 是否相同,并检查 tokenId 是否不为 null,因为在某些情况下在某些情况下,它可能只是 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/有什么想法吗?

最佳答案

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)

关于java - 在android中比较两个字符串如url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com