从scala字符串中删除特定部分

时间:2015-06-01 20:29:35

标签: scala

我有以下字符串

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

我想要提取的是GETHTTP/1.1之间的部分,因此在此示例中访问的网址为/hello

我该怎么做?

2 个答案:

答案 0 :(得分:1)

这是你想要的吗?

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

答案 1 :(得分:0)

这是另一种方法

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

} }