Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在球拍中查找子串的索引_String_Racket - Fatal编程技术网

String 在球拍中查找子串的索引

String 在球拍中查找子串的索引,string,racket,String,Racket,我通读了一遍,但找不到与Java/JavaScript方法等效的函数。也就是说,是否有一个函数返回较大字符串中子字符串的第一个字符的索引;最好是采用可选的启动参数。比如: (string-index "foobar" "bar") ;;; returns 3 我很乐意使用类似列表的函数。在Racket中没有这样的原始函数,但您可以使用,例如: (regexp-match-positions "example" "This is an example.") => '((11 . 18))

我通读了一遍,但找不到与Java/JavaScript方法等效的函数。也就是说,是否有一个函数返回较大字符串中子字符串的第一个字符的索引;最好是采用可选的启动参数。比如:

(string-index "foobar" "bar")
;;; returns 3

我很乐意使用类似列表的函数。

在Racket中没有这样的原始函数,但您可以使用,例如:

(regexp-match-positions "example" "This is an example.")
=> '((11 . 18))

SRFI 13中有很多字符串操作可用。其中包括
string包含的
,它完全满足您的需求

#lang racket
(require srfi/13) ; the string SRFI    
(string-contains "foobar" "bar")   ; evaluates to 3
请参阅此处的更多信息:

FWIW这里是
字符串索引的一个简单实现

(define (string-index hay needle)
  (define n (string-length needle))
  (define h (string-length hay))
  (and (<= n h) ; if the needle is longer than hay, then the needle can not be found
       (for/or ([i (- h n -1)]
                #:when (string=? (substring hay i (+ i n)) needle))
         i)))

(string-index "foobar" "bar")
(定义(字符串索引)
(定义n(线长度针))
(定义h(字符串长度))

(和)(我认为你不会对像member这样的东西感到满意,它检查序列的成员;你想检查子序列。