【问题标题】:Finding a substring within a list of strings in clojure在clojure中的字符串列表中查找子字符串
【发布时间】:2021-11-15 21:49:14
【问题描述】:

我想查找我的字符串列表中的任何字符串中是否有子字符串。我有一个类似 '("hi" "hey" "hello") 的列表。使用“some”我可以找到值“hi”是否在此列表中。但是我怎么能找到“h”是否在列表中的至少一个字符串中?

【问题讨论】:

  • 如何使用some检查"hi"是否在列表中?
  • 类似这样的东西,我猜(some #(clojure.string/includes? % "h") ["hi" "hello" "hey"])
  • 请参阅此文档列表,尤其是。 Clojure 备忘单:github.com/io-tupelo/clj-template#documentation

标签: clojure clojurescript


【解决方案1】:

Clojure 解决方案:

(some #(clojure.string/includes? % "h") (list "hi" "hello" "hey"))
(some #(.contains % "h") (list "hi" "hello" "hey"))

ClojureScript 解决方案:

  • includes? 来自 clojure.string(需要 clojure.stringns 形式)
(ns my-app.core
  (:require [clojure.string :as string]))

(some #(string/includes? % "h") (list "hi" "hello" "hey"))
(some #(.includes % "h") (list "hi" "hello" "hey"))

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 2023-04-03
    • 2018-09-27
    • 2021-04-21
    • 2019-06-22
    • 1970-01-01
    • 2011-04-10
    • 2016-10-21
    相关资源
    最近更新 更多