Scala - 从单个String中删除字符串列表/映射中的所有元素

时间:2018-03-28 14:54:08

标签: string scala foldleft

在内部网站上工作,其中URL包含来自其他系统的源参考。这是业务要求,无法更改。 即“http://localhost:9000/source.address.com/7808/project/repo” “http://localhost:9000/build.address.com/17808/project/repo

我需要使用特征从“project / repo”字符串/变量中删除这些字符串,以便可以从多个服务本地使用它。我还希望能够向此列表添加更多源(已存在),而不是修改方法。

“normal normalizePath”是服务访问的方法,目前为止是非理想但合理的尝试。使用foldLeft卡在上面,我喜欢一些帮助,或者更简单的方法来完成所描述的操作。代码示例如下。

第一次尝试使用if-else(不理想,因为需要在行下添加更多if / else语句,并且比模式匹配更不易读)

trait NormalizePath {
    def normalizePath(path: String): String = {
        if (path.startsWith("build.address.com/17808")) {
            path.substring("build.address.com/17808".length, path.length)
        } else {
            path
        }
    }
}

和第二次尝试(不太理想,因为可能会添加更多模式,并且它生成的字节码比if / else更多)

trait NormalizePath {
    val pattern = "build.address.com/17808/"
    val pattern2 = "source.address.com/7808/"
    def normalizePath(path: String) = path match {
        case s if s.startsWith(pattern) => s.substring(pattern.length, s.length)
        case s if s.startsWith(pattern2) => s.substring(pattern2.length, s.length)
        case _ => path
    }
}

最后一次尝试是使用地址列表(已在其他地方存在但在此处定义为MWE)从路径字符串中删除出现的内容并且它不起作用:

trait NormalizePath {
    val replacements = (
        "build.address.com/17808",
        "source.address.com/7808/")

    private def remove(path: String, string: String) = {
        path-string
    }

    def normalizePath(path: String): String = {
        replacements.foldLeft(path)(remove)
    }
}   

感谢您的任何帮助!

3 个答案:

答案 0 :(得分:3)

如果您只是剥离这些字符串:

val replacements = Seq(
  "build.address.com/17808",
  "source.address.com/7808/")


replacements.foldLeft("http://localhost:9000/source.address.com/7808/project/repo"){
  case(path, toReplace) => path.replaceAll(toReplace, "")
}
// http://localhost:9000/project/repo

如果你用其他东西替换这些字符串:

val replacementsMap = Seq(
  "build.address.com/17808" -> "one",
  "source.address.com/7808/" -> "two/")


replacementsMap.foldLeft("http://localhost:9000/source.address.com/7808/project/repo"){
  case(path, (toReplace, replacement)) => path.replaceAll(toReplace, replacement)
}
// http://localhost:9000/two/project/repo

replacements集合可以来自代码中的其他位置,无需重新部署。

// method replacing by empty string
def normalizePath(path: String) = {
  replacements.foldLeft(path){
    case(startingPoint, toReplace) => startingPoint.replaceAll(toReplace, "")
  }
}

normalizePath("foobar/build.address.com/17808/project/repo")
// foobar/project/repo

normalizePath("whateverPath")
// whateverPath

normalizePath("build.address.com/17808build.address.com/17808/project/repo")
// /project/repo

答案 1 :(得分:2)

有一百万种方法可以从Scala中的String中提取/project/repo。以下是我想出的一些内容:

val list = List("build.address.com/17808", "source.address.com/7808") //etc
def normalizePath(path: String) = {
  path.stripPrefix(list.find(x => path.contains(x)).getOrElse(""))
}

输出:

scala> normalizePath("build.address.com/17808/project/repo")
res0: String = /project/repo
val list = List("build.address.com/17808", "source.address.com/7808") //etc
def normalizePath(path: String) = {
  list.map(x => if (path.contains(x)) {
    path.takeRight(path.length - x.length)
  }).filter(y => y != ()).head
}

输出:

scala> normalizePath("build.address.com/17808/project/repo")
res0: Any = /project/repo
val list = List("build.address.com/17808", "source.address.com/7808") //etc
def normalizePath(path: String) = {
  list.foldLeft(path)((a, b) => a.replace(b, ""))
}

输出:

scala> normalizePath("build.address.com/17808/project/repo")
res0: String = /project/repo

真的,取决于你希望你的代码看起来多么复杂(或者你想要多么愚蠢)。请注意,第二个示例的返回类型为Any,这可能不适合您的方案。此外,这些示例并不意味着只能将字符串从path中间取出......如果您想这样做,它们可以相当容易地修改。如果您希望我添加一些示例,只需从字符串中删除build.address.com/17808之类的内容,请告诉我 - 我很乐意这样做。

答案 2 :(得分:1)

可以通过以下方式进行非常简单的替换:

val replacements = Seq(
  "build.address.com/17808",
  "source.address.com/7808/")

def normalizePath(path: String): String = {
  replacements.find(path.startsWith(_)) // find the first occurrence
              .map(prefix => path.substring(prefix.length)) // remove the prefix
              .getOrElse(path) // if not found, return the original string
}

由于预期的替换非常相似,您是否尝试对它们进行概括并使用正则表达式匹配?