String.Cut
type String.Cut = { before: string, after: string }String.Cut is the two sides of a string around a separator, as String.cut returns them.
Details
String.cutreturns aString.Cutwhen the separator occurs, withbeforethe text in front of its first occurrence andafterthe text that follows it, andnullwhen it does not.
Basic examples
Get a String.Cut from String.cut, check it for null, and read its fields:
for line in ["name=ant", "oops"] {
let c = line.cut("=")
if c != null {
println("${c.before} -> ${c.after}")
} else {
println("no '=' in ${line}")
}
}name -> ant
no '=' in oopsOnly the first separator splits; after keeps the later ones, and a separator at an end leaves an empty field:
String.cut("k=v=w", "=") // String.Cut{ before: "k", after: "v=w" }
String.cut("=v", "=") // String.Cut{ before: "", after: "v" }Write one with a literal:
String.Cut{ before: "k", after: "v" } // String.Cut{ before: "k", after: "v" }Destructure the fields into variables:
let { before, after } = String.cut("host:8080", ":") ?? return
println(before)
println(after)host
8080Scope
Rename the fields while destructuring:
let { before: key, after: value } = String.cut("lang=ant", "=") ?? String.Cut{ before: "", after: "" }
key // "lang"
value // "ant"Annotate a variable, a parameter, or a return type with String.Cut:
func pair(s: string, sep: string): String.Cut {
String.cut(s, sep) ?? String.Cut{ before: s, after: "" }
}
let p: String.Cut = pair("a:b", ":")
println(p)
println(pair("a", ":"))String.Cut{ before: "a", after: "b" }
String.Cut{ before: "a", after: "" }Assign to a field, as with any record:
let c = String.Cut{ before: "k", after: "v" }
c.after = "w"
c // String.Cut{ before: "k", after: "w" }Copy one with a spread and change a field:
let c = String.Cut{ before: "k", after: "v" }
String.Cut{ ...c, before: "key" } // String.Cut{ before: "key", after: "v" }Test a union member with is:
func describe(x: String.Cut | string): string {
if x is String.Cut { "${x.before} | ${x.after}" } else { x }
}
println(describe(String.Cut{ before: "a", after: "b" }))
println(describe("whole"))a | b
wholeAn array of results holds String.Cut records and nulls:
["a=1", "b=2", "c"].map({ x.cut("=") }) // [String.Cut{ before: "a", after: "1" }, String.Cut{ before: "b", after: "2" }, null]A function that takes an anonymous record type accepts a String.Cut with the fields it lists:
func head(x: { before: string }): string { x.before }
head(String.Cut{ before: "a", after: "b" }) // "a"String.from and Json.stringify show both fields:
let c = String.Cut{ before: "k", after: "v" }
String.from(c) // "String.Cut{ before: \"k\", after: \"v\" }"
Json.stringify(c) // "{\"before\":\"k\",\"after\":\"v\"}"Applications
Split user@domain addresses and group the users by domain:
let users: Map[string, Array[string]] = Map{}
for address in ["ann@a.org", "bob@b.org", "cat@a.org", "nobody"] {
let { before: user, after: domain } = address.cut("@") ?? continue
let list = users[domain] ?? []
list.push(user)
users[domain] = list
}
println(users)Map{ "a.org": ["ann", "cat"], "b.org": ["bob"] }Return a String.Cut from your own function, here one that cuts at the last separator:
func cutLast(s: string, sep: string): String.Cut | null {
let i = s.lastIndexOf(sep) ?? return null
String.Cut{ before: s.slice(0, i), after: s.slice(i + sep.length(), s.length()) }
}
println(cutLast("archive.tar.gz", "."))
println(cutLast("README", "."))String.Cut{ before: "archive.tar", after: "gz" }
nullProperties and relations
Two String.Cut records are equal when their fields are equal:
assert(String.cut("k=v", "=") == String.Cut{ before: "k", after: "v" })
assert(String.Cut{ before: "a", after: "b" } != String.Cut{ before: "b", after: "a" })Assigning a String.Cut shares it; a spread makes a copy:
let a = String.Cut{ before: "k", after: "v" }
let b = a
let copy = String.Cut{ ...a }
b.after = "w"
assert(a.after == "w")
assert(copy.after == "v")
assert(Record.same(a, b))
assert(!Record.same(a, copy))Possible issues
The type lives in the String namespace. Cut alone is not a type:
let c: Cut = String.Cut{ before: "a", after: "b" } // error: unknown type CutA literal gives both fields:
let c = String.Cut{ before: "a" } // error: String.Cut literal is missing the field afterString.cut returns String.Cut | null, so reading a field before narrowing is a compile error:
let c = String.cut("a=b", "=")
println(c.before) // error: String.Cut | null has no field before: narrow the value firstA record of your own with the same fields is a different type. It does not compare with a String.Cut, and a spread converts it:
type Pair = { before: string, after: string }
let p = Pair{ before: "k", after: "v" }
println(p == String.Cut{ before: "k", after: "v" })
// error: Pair and String.Cut cannot be compared with ==: neither is assignable to the othertype Pair = { before: string, after: string }
let p = Pair{ before: "k", after: "v" }
String.Cut{ ...p } == String.Cut{ before: "k", after: "v" } // trueSee also
String.cut · String.split · String.indexOf · String.lastIndexOf · Indexed · Entry
