Back

last-index-of (clj)

(source)

function

(last-index-of s value) (last-index-of s value from-index)
Return last index of value (string or char) in s, optionally searching backward from from-index. Return nil if value not found.

Examples

clojure
(ns clojure.test-clojure.string
  (:require [clojure.string :as s])
  (:use clojure.test))

(deftest t-last-index-of
  (let [sb (StringBuffer. "banana")]
    (is (= 4 (s/last-index-of sb "n")))
    (is (= 4 (s/last-index-of sb \n)))
    (is (= 3 (s/last-index-of sb "an")))
    (is (= 4 (s/last-index-of sb "n" )))
    (is (= 4 (s/last-index-of sb "n" 5)))
    (is (= 4 (s/last-index-of sb \n  5)))
    (is (= 4 (s/last-index-of sb "n" 500)))
    (is (= nil (s/last-index-of sb "z")))
    (is (= nil (s/last-index-of sb "z" 1)))
    (is (= nil (s/last-index-of sb \z  1)))
    (is (= nil (s/last-index-of sb "z" 100))
    (is (= nil (s/last-index-of sb "z" -10))))))
clojure/clojure
(ns clojure.test-clojure.string
  (:require [clojure.string :as s])
  (:use clojure.test))

(deftest t-last-index-of
  (let [sb (StringBuffer. "banana")]
    (is (= 4 (s/last-index-of sb "n")))
    (is (= 4 (s/last-index-of sb \n)))
    (is (= 3 (s/last-index-of sb "an")))
    (is (= 4 (s/last-index-of sb "n" )))
    (is (= 4 (s/last-index-of sb "n" 5)))
    (is (= 4 (s/last-index-of sb \n  5)))
    (is (= 4 (s/last-index-of sb "n" 500)))
    (is (= nil (s/last-index-of sb "z")))
    (is (= nil (s/last-index-of sb "z" 1)))
    (is (= nil (s/last-index-of sb \z  1)))
    (is (= nil (s/last-index-of sb "z" 100))
    (is (= nil (s/last-index-of sb "z" -10))))))
clojure/clojurescript
(ns clojure.string-test
  (:require [cljs.test :as test
             :refer-macros [deftest is testing]]
            [clojure.test.check :as tc]
            [clojure.test.check.clojure-test :refer-macros [defspec]]
            [clojure.test.check.generators :as gen]
            [clojure.test.check.properties :as prop :include-macros true]
            [clojure.string :as s]))

  (testing "Testing string last-index-of"
    (let [sb "banana"]
      (is (= 4 (s/last-index-of sb "n")))
      (is (= 4 (s/last-index-of sb \n)))
      (is (= 3 (s/last-index-of sb "an")))
      (is (= 4 (s/last-index-of sb "n" )))
      (is (= 4 (s/last-index-of sb "n" 5)))
      (is (= 4 (s/last-index-of sb \n  5)))
      (is (= 4 (s/last-index-of sb "n" 500)))
      (is (= nil (s/last-index-of sb "z")))
      (is (= nil (s/last-index-of sb "z" 1)))
      (is (= nil (s/last-index-of sb \z  1)))
      (is (= nil (s/last-index-of sb "z" 100)))
      (is (= nil (s/last-index-of sb "z" -10)))))
functional-koans/clojure-koans
(ns koans.02-strings
  (:require [koan-engine.core :refer :all]
            [clojure.string :as string]))

  "Or maybe the last index of the same substring"
  (= __ (string/last-index-of "hello world, hello" "hello"))
arcadia-unity/Arcadia
(ns clojure.test-clojure.string
  (:require [clojure.string :as s])
  (:use clojure.test))

(deftest t-last-index-of
  (let [sb "banana"]                                        ;;; (StringBuffer. "banana")  We don't have CharSequence, no need to work with a StringBuilder here
    (is (= 4 (s/last-index-of sb "n")))
    (is (= 4 (s/last-index-of sb \n)))
    (is (= 3 (s/last-index-of sb "an")))
    (is (= 4 (s/last-index-of sb "n" )))
    (is (= 4 (s/last-index-of sb "n" 5)))
    (is (= 4 (s/last-index-of sb \n  5)))
    #_(is (= 4 (s/last-index-of sb "n" 500)))               ;;; negative index not valid
    (is (= nil (s/last-index-of sb "z")))
    (is (= nil (s/last-index-of sb "z" 1)))
    (is (= nil (s/last-index-of sb \z  1)))
    #_(is (= nil (s/last-index-of sb "z" 100))              ;;; negative index not valid
    #_(is (= nil (s/last-index-of sb "z" -10))))))          ;;; negative index not valid
rekola/nanoclj
(require '[ clojure.test :as t ]
         '[ clojure.string :as str ])

(t/is (= (str/index-of "Hélen Hélen" "len") 2))
(t/is (= (str/last-index-of "Hélen Hélen" "len") 8))