Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings like url in android

I have two strings:

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

and

http://porter.com/request/tokenId

I want to check if first parts: http://porter.com/request are the same in both and check if tokenId is not null, because in some cases it could be only http://porter.com/request/. I use something like that:

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
    }

but this is not working for last case: http://porter.com/request/ Any ideas?

like image 855
edi233 Avatar asked May 07 '26 23:05

edi233


1 Answers

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)
like image 156
GensaGames Avatar answered May 10 '26 14:05

GensaGames



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!