On this page:
28.1 Contracts and Predicates
text/ c
text?
string-literal?
bytes-literal?
keyword-literal?
28.2 Text Conversions and Concatenation
text->string
text->bytes
text->symbol
text->keyword
text->string-literal
text->bytes-literal
text->identifier
text->keyword-literal
28.3 Text Comparisons
text=?
text<?
text<=?
text>?
text>=?
Version: 5.0.2

28 Text Representations

Carl Eastlund <cce@racket-lang.org>

 (require unstable/text)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

This module provides tools for manipulating and converting textual data.

28.1 Contracts and Predicates

text/c : flat-contract?
(text? v)  boolean?
  v : any/c
This contract and predicate recognize text values: strings, byte strings, symbols, and keywords, as well as syntax objects containing them.

Examples:

  > (text? "text")

  #t

  > (text? #"text")

  #t

  > (text? 'text)

  #t

  > (text? '#:text)

  #t

  > (text? #'"text")

  #t

  > (text? #'#"text")

  #t

  > (text? #'text)

  #t

  > (text? #'#:text)

  #t

  > (text? '(not text))

  #f

(string-literal? v)  boolean?
  v : any/c
(bytes-literal? v)  boolean?
  v : any/c
(keyword-literal? v)  boolean?
  v : any/c
These predicates recognize specific text types stored in syntax objects.

Examples:

  > (string-literal? #'"literal")

  #t

  > (string-literal? "not literal")

  #f

  > (bytes-literal? #'#"literal")

  #t

  > (bytes-literal? #"not literal")

  #f

  > (keyword-literal? #'#:literal)

  #t

  > (keyword-literal? '#:not-literal)

  #f

28.2 Text Conversions and Concatenation

(text->string [#:before before    
  #:between between    
  #:after after]    
  text ...)  string?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  text : text/c
(text->bytes [#:before before    
  #:between between    
  #:after after]    
  text ...)  bytes?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  text : text/c
(text->symbol [#:before before    
  #:between between    
  #:after after]    
  text ...)  symbol?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  text : text/c
(text->keyword [#:before before    
  #:between between    
  #:after after]    
  text ...)  keyword?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  text : text/c
These functions convert text values to specific types. They concatenate each text argument, adding before and after to the front and back of the result and between between each argument.

Examples:

  > (text->string #"concat" #'enate)

  "concatenate"

  > (text->bytes #:between "-" 'concat #'#:enate)

  #"concat-enate"

  > (text->symbol #:before "(" #:after ")" '#:concat #'"enate")

  '|(concatenate)|

  > (text->keyword #:before #'< #:between #'- #:after #'> "concat" #'#"enate")

  '#:<concat-enate>

(text->string-literal [#:before before    
  #:between between    
  #:after after    
  #:stx stx]    
  text ...)  string-literal?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  stx : (or/c syntax? false/c) = #f
  text : text/c
(text->bytes-literal [#:before before    
  #:between between    
  #:after after    
  #:stx stx]    
  text ...)  bytes-literal?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  stx : (or/c syntax? false/c) = #f
  text : text/c
(text->identifier [#:before before    
  #:between between    
  #:after after    
  #:stx stx]    
  text ...)  identifier?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  stx : (or/c syntax? false/c) = #f
  text : text/c
(text->keyword-literal [#:before before    
  #:between between    
  #:after after    
  #:stx stx]    
  text ...)  keyword-literal?
  before : text/c = ""
  between : text/c = ""
  after : text/c = ""
  stx : (or/c syntax? false/c) = #f
  text : text/c
These functions convert text values to specific syntax object types, deriving syntax object properties from the stx argument. They concatenate each text argument, adding before and after to the front and back of the result and between between each argument.

Examples:

  > (text->string-literal #"concat" #'enate)

  #<syntax "concatenate">

  > (text->bytes-literal #:between "-" 'concat #'#:enate)

  #<syntax #"concat-enate">

  > (text->identifier #:before "(" #:after ")"
                       #:stx #'props
                      '#:concat #'"enate")

  #<syntax:4:0 |(concatenate)|>

  > (text->keyword-literal #:before #'< #:between #'- #:after #'>
                           #:stx #'props
                           "concat" #'#"enate")

  #<syntax:5:0 #:<concat-enate>>

28.3 Text Comparisons

(text=? one two)  boolean?
  one : text/c
  two : text/c
(text<? one two)  boolean?
  one : text/c
  two : text/c
(text<=? one two)  boolean?
  one : text/c
  two : text/c
(text>? one two)  boolean?
  one : text/c
  two : text/c
(text>=? one two)  boolean?
  one : text/c
  two : text/c
These predicates compare the character content of two text values. They are equivalent to:

  (text=? one two) = (string=? (text->string one) (text->string two))
  (text<? one two) = (string<? (text->string one) (text->string two))
  (text<=? one two) = (string<=? (text->string one) (text->string two))
  (text>? one two) = (string>? (text->string one) (text->string two))
  (text>=? one two) = (string>=? (text->string one) (text->string two))

Examples:

  > (text=? #"x" #'y)

  #f

  > (text<? #"x" #'y)

  #t

  > (text<=? #"x" #'y)

  #t

  > (text>? #"x" #'y)

  #f

  > (text>=? #"x" #'y)

  #f