0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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())

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.