我只是想知道是否有字符串拆分功能?类似于:
> (string-split "19 2.14 + 4.5 2 4.3 / - *")
'("19" "2.14" "+" "4.5" "2" "4.3" "/" "-" "*")我还没有找到它,并创建了我自己的。我经常使用Scheme,所以如果你修复它并提出更好的解决方案,我将不胜感激:
#lang racket
(define expression "19 2.14 + 4.5 2 4.3 / - *")
(define (string-split str)
(define (char->string c)
(make-string 1 c))
(define (string-first-char str)
(string-ref str 0))
(define (string-first str)
(char->string (string-ref str 0)))
(define (string-rest str)
(substring str 1 (string-length str)))
(define (string-split-helper str chunk lst)
(cond
[(string=? str "") (reverse (cons chunk lst))]
[else
(cond
[(char=? (string-first-char str) #\space) (string-split-helper (string-rest str) "" (cons chunk lst))]
[else
(string-split-helper (string-rest str) (string-append chunk (string-first str)) lst)]
)
]
)
)
(string-split-helper str "" empty)
)
(string-split expression)发布于 2011-10-10 10:32:29
为了给其他策划者参考,我在鸡方案中使用irregex鸡蛋做了以下事情:
(use irregex)
(define split-regex
(irregex '(+ whitespace)))
(define (split-line line)
(irregex-split split-regex line))
(split-line "19 2.14 + 4.5 2 4.3 / - *") =>
("19" "2.14" "+" "4.5" "2" "4.3" "/" "-" "*")发布于 2015-05-17 15:32:13
嗯,您可以使用普通的旧字符串拆分
> (string-split "19 2.14 + 4.5 2 4.3 / - *")
'("19" "2.14" "+" "4.5" "2" "4.3" "/" "-" "*")它是球拍http://docs.racket-lang.org/reference/strings.html#%28def._%28%28lib._racket%2Fstring..rkt%29._string-split%29%29的一部分
https://stackoverflow.com/questions/7691769
复制相似问题