I need to add prefix string to my link with is returned by attribute "href".
.getElementsByClass(CLASS_OFFER_NAME)
.firstOrNull()
?.attr("href") // Here need I add prefix to attr href
.orEmpty()
I getting now url eg: work/london/22 but I need add prefix with domain so I want to get www.offerlist.com/work/london/22
May be you can convert String?
to Optional<T>
and apply prefix concatenation though Optional.map
method and finally extract the resulting value using orElse("")
...
This is a functional approach to the problem, but you can obviously solve it via if
or ternary or elvis operator applied to the value returned by orEmpty
.
If you like the functional approach, you can give a try to this example, which shows both Optional and T? --> Optional use cases:
fun <T> toOptional(value : T?) : Optional<T> {
if(value == null) {
return Optional.empty()
}
return Optional.of(value)
}
val opt:Optional<String> = Optional.empty()
println(opt.map { x -> "PREFIX/$x" }.orElse(""))
val opt2:Optional<String> = Optional.of("link")
println(opt2.map { x -> "PREFIX/$x" }.orElse(""))
var str : String? = null
println(toOptional(str).map { x -> "PREFIX/$x" }.orElse(""))
var str2 = "hrefValue"
println(toOptional(str2).map { x -> "PREFIX/$x" }.orElse(""))
I hope this can help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.