Branka
Branka

Reputation: 125

Strip specific part from scala string

I have the following string

"GET /hello HTTP/1.1
User-Agent: Wget/1.16.1 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: localhost:8008
Connection: Keep-Alive"

All I want to extract is the part between GET and HTTP/1.1, so the URL that is getting accessed, /hello in this example.

How can I do this?

Upvotes: 0

Views: 88

Answers (2)

Vamsi Krishna
Vamsi Krishna

Reputation: 1

Here is another way of doing it

object ContetExtractor {
main(args: Array[String]) {
val givenString = "GET /hello HTTP/1.1"
val from = "GET";
val to = "HTTP"
println(givenString.slice(from.length(),givenString.indexOfSlice(to)).trim())

} }

Upvotes: 0

Sudheer Aedama
Sudheer Aedama

Reputation: 2144

Is this what you want ?

scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.

scala> "GET /hello HTTP/1.1"
res0: String = GET /hello HTTP/1.1

scala> res0.split(" ")
res1: Array[String] = Array(GET, /hello, HTTP/1.1)

scala> res1(1) // Note that this is unsafe
res2: String = /hello

Upvotes: 1

Related Questions