user4433284
user4433284

Reputation:

Set value to a variable that needs a replace and result of if, is it possible in one line?

Sorry newbie question, I'm learnig scala

So i got a href say href="www.bbc.co.uk/", and i want to get rid of the href and /. whats the most conside way of doing it?

This is what i got so far

var cleandUpUrl = "href=".r.replaceFirstIn(urls, "").replace("\"", "")
cleandUpUrl = if(cleandUpUrl.endsWith("/")) cleandUpUrl.dropRight(1) else cleandUpUrl

I was hoping to be able to do something like

val cleandUpUrl = if ("href=".r.replaceFirstIn(urls, "").replace("\"", "").endsWith("/")) _.dropRight(1) else _ 

Somehow return the value created inside the if.

Upvotes: 1

Views: 52

Answers (1)

akuiper
akuiper

Reputation: 214957

You can try replaceAll with regex href=|/ which matches either href= or /:

val urls = "href=\"www.bbc.co.uk/\""
// urls: String = href="www.bbc.co.uk/"

urls.replaceAll("href=|/", "")
// res106: String = "www.bbc.co.uk"

Suppose you just want to replace the trailing /, you can use look ahead syntax to replace the / right before ":

urls.replaceAll("href=|/(?=\")", "")
// res111: String = "www.bbc.co.uk"

val urls = "href=\"www.bbc.co.uk/iplayer/sports = www.bbc.co.ukiplayersports\""
// urls: String = href="www.bbc.co.uk/iplayer/sports = www.bbc.co.ukiplayersports"

urls.replaceAll("href=|/(?=\")", "")
// res113: String = "www.bbc.co.uk/iplayer/sports = www.bbc.co.ukiplayersports"

Upvotes: 1

Related Questions