Claudiu
Claudiu

Reputation: 229461

Check string containment in Scheme

How do I check, in DrScheme, whether a string contains a given character / substring? How do I include the proper module if it is defined in a module?

Upvotes: 2

Views: 1095

Answers (3)

Mark Bolusmjak
Mark Bolusmjak

Reputation: 24409

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

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

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

Upvotes: 1

Mark Bolusmjak
Mark Bolusmjak

Reputation: 24409

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)))))))

Upvotes: 0

Vijay Mathew
Vijay Mathew

Reputation: 27184

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.

Upvotes: 0

Related Questions