首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串拆分函数

字符串拆分函数
EN

Stack Overflow用户
提问于 2011-10-08 03:06:46
回答 2查看 10.5K关注 0票数 10

我只是想知道是否有字符串拆分功能?类似于:

代码语言:javascript
复制
> (string-split "19 2.14 + 4.5 2 4.3 / - *")
'("19" "2.14" "+" "4.5" "2" "4.3" "/" "-" "*")

我还没有找到它,并创建了我自己的。我经常使用Scheme,所以如果你修复它并提出更好的解决方案,我将不胜感激:

代码语言:javascript
复制
#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)
EN

回答 2

Stack Overflow用户

发布于 2011-10-10 10:32:29

为了给其他策划者参考,我在鸡方案中使用irregex鸡蛋做了以下事情:

代码语言:javascript
复制
(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" "/" "-" "*")
票数 7
EN

Stack Overflow用户

发布于 2015-05-17 15:32:13

嗯,您可以使用普通的旧字符串拆分

代码语言:javascript
复制
> (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的一部分

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7691769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档