开发者

Check string containment in Scheme

How do I check, in DrScheme, whether a string contains a given character / sub开发者_如何学JAVAstring? How do I include the proper module if it is defined in a module?


In DrScheme, assuming the language is set to "Module", the following will work

#lang scheme
(require (lib "13.ss" "srfi"))

(string-contains "1234abc"  "abc")


There is no standard procedure to do this. SRFI 13 contain the procedure you need (string-index). Please check if your Scheme implements this SRFI.


Here is a quick hack. It returns the index (0 based) of string s in string t. Or #f if not found. Probably not the best way to do it if your Scheme has SRFI-13 support, or other built-in support.

Code edited. Thanks to Eli for suggestions.

(define (string-index s t)
  (let* ((len (string-length s))
        (max (- (string-length t) len)))        
    (let loop ((i 0))
      (cond ((> i max) 
             #f)
            ((string=? s
                       (substring t i (+ i len)))
             i)
            (else (loop (+ i 1)))))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜