11.7. text: Operations on strings.

Operations on strings.

11.7.1. String

site String

Strings themselves have a set of methods associated with them. These methods can be invoked on any string literal or any variable bound to a string.

The methods documented here are only a subset of those available in the Java implementation. In practice, strings in the Java implementation support all methods provided by Java's String class.

string.length() :: Integer

Return the length of the string.


-- Publishes: 4

string.substring(Integer, Integer) :: String

Return the substring of this string covered by the given half-open range.


-- Publishes: "orc"
val s = "apple orchard"

string.indexOf(String) :: Integer

Return the starting index of the first occurrence of the given string.


-- Publishes: 6
"apple orchard".indexOf("orc")

11.7.2. Print

site Print(Top) :: Signal


Print a value as a string to standard output. For Java objects, this will call toString() to convert the object to a String.

11.7.3. Println

site Println(Top) :: Signal


Same as Print, appending a newline.

11.7.4. Read

site Read[A](String) :: A


Given a string representing an Orc value (using standard Orc literal syntax), return the corresponding value. If the argument does not conform to Orc literal syntax, halt with an error.


  Read("true") -- publishes the boolean true
| Read("1") -- publishes the integer 1
| Read("(3.0, [])") -- publishes the tuple (3.0, [])
| Read("\"hi\"") -- publishes the string "hi"

11.7.5. Write

site Write(Top) :: String


Given an Orc value, return its string representation using standard Orc literal syntax. If the value is of a type with no literal syntax, (for example, it is a site), return an arbitrary string representation which is intended to be human-readable.


  Write(true) -- publishes "true"
| Write(1) -- publishes "1"
| Write((3.0, [])) -- publishes "(3.0, [])"
| Write("hi") -- publishes "\"hi\""

11.7.6. lines

def lines(String) :: List[String]

Split a string into lines, which are substrings terminated by an endline or the end of the string. DOS, Mac, and Unix endline conventions are all accepted. Endline characters are not included in the result.


def lines(String) :: List[String]
def lines(text) = arrayToList(text.split("\n|\r\n|\r"))


11.7.7. unlines

def unlines(List[String]) :: String

Append a linefeed, "\n", to each string in the sequence and concatenate the results.


def unlines(List[String]) :: String
def unlines(line:lines) = line + "\n" + unlines(lines)
def unlines([]) = ""


11.7.8. words

def words(String) :: List[String]

Split a string into words, which are sequences of non-whitespace characters separated by whitespace.



def words(String) :: List[String]
def words(text) = arrayToList(text.trim().split("\\s+"))


11.7.9. unwords

def unwords(List[String]) :: String

Concatenate a sequence of strings with a single space between each string.


def unwords(List[String]) :: String
def unwords([]) = ""
def unwords([word]) = word
def unwords(word:words) = word + " " + unwords(words)


11.7.10. characters

def characters(String) :: List[String]

Convert a string to a list of strings of length 1, where the list items are the characters of the given string.

Example: characters("Hello") = ["H", "e", "l", "l", "o"]


def characters(String) :: List[String]
def characters(s) =
  def rest(Integer) :: List[String]
  def rest(i) =
      Ift (i >= s.length()) >> []
    | Ift (i <: s.length()) >> s.substring(i,i+1):rest(i+1)