String.isSymbol
func String.isSymbol(s: string): boolString.isSymbol reports whether every code point of s is a symbol (S). It is false on "".
Details
truewhen every code point ofshas a general category in S (Sm,Sc,Sk,So);falseon"".- Many ASCII signs, such as
%,#,&,*,@, and-, are punctuation, not symbols.
Basic examples
String.isSymbol("+") // true
String.isSymbol("$") // true
String.isSymbol("©") // true
String.isSymbol("a") // falseCall it through .:
"<=>".isSymbol() // trueScope
The empty string has no code points, so it is not all symbols:
String.isSymbol("") // falseOne code point that is not a symbol makes the result false, wherever it is in the string:
String.isSymbol("$5") // false
String.isSymbol("+++") // true
String.isSymbol("1+1") // falseEach of the four kinds of symbol counts. String.category names the kind:
String.isSymbol("√") // true
String.category("√") // "Sm"
String.isSymbol("€") // true
String.category("€") // "Sc"
String.isSymbol("^") // true
String.category("^") // "Sk"
String.isSymbol("°") // true
String.category("°") // "So"The ASCII symbols are $, +, <, =, >, ^, `, |, and ~:
"$+<=>^`|~".chars().every({ x.isSymbol() }) // trueAn emoji of one code point is a symbol, and so is a flag, which is two regional indicator symbols. A skin tone modifier is a modifier symbol, so a thumbs-up with a skin tone is all symbols too:
String.isSymbol("😀") // true
String.isSymbol("🇫🇷") // true
String.isSymbol("👍🏽") // trueLetters, digits, other numbers, and white space are not symbols:
String.isSymbol("x") // false
String.isSymbol("7") // false
String.isSymbol("½") // false
String.isSymbol(" ") // falseTest each code point of a string with String.chars in a pipe:
"2+2=4" | String.chars() | Array.map({ x.isSymbol() }) // [false, true, false, true, false]Applications
Split a price into its currency sign and its amount. The sign is a symbol; the digits are not:
type Price = { currency: string, amount: string }
func parsePrice(s: string): Price | null {
let chars = s.chars()
if chars.isEmpty() || !chars[0].isSymbol() {
return null
}
Price{ currency: chars[0], amount: s.trimPrefix(chars[0]) }
}
parsePrice("€25") // Price{ currency: "€", amount: "25" }
parsePrice("25") // nullTell an operator token from a name in a small expression scanner. Operators mix symbols such as + and < with punctuation such as - and &, so test each code point for either:
func isOperator(token: string): bool {
!token.isEmpty() && token.chars().every({ x.isSymbol() || x.isPunct() })
}
for token in ["x", "+", "-", "<=", "&&", "count", "|>"] {
let kind = if isOperator(token) { "operator" } else { "name" }
println("${kind} ${token}")
}name x
operator +
operator -
operator <=
operator &&
name count
operator |>Count the symbols of a text:
"1 + 2 = 3 ≠ 4 ✓".chars().countWhere({ x.isSymbol() }) // 4Properties and relations
String.isSymbol(s) is s not empty and every code point in a category that starts with S:
for s in ["", "+", "$5", "😀", "©®", "%", "a", "👍🏽"] {
let chars = s.chars()
let all = !chars.isEmpty() && chars.every({ x.category().startsWith("S") })
assert(String.isSymbol(s) == all)
}A code point is never both a symbol and punctuation, a letter, or a number. Every symbol is graphic and printable:
for c in ["+", "$", "^", "©", "😀", "%", "a", "1"] {
if String.isSymbol(c) {
assert(!String.isPunct(c) && !String.isLetter(c) && !String.isNumber(c))
assert(String.isGraphic(c) && String.isPrint(c))
}
}Possible issues
Many ASCII signs are punctuation, not symbols. To accept both, test both on each code point. Testing the whole string with each function fails on a string that mixes the two, such as !=:
String.isSymbol("%") // false
String.isPunct("%") // true
String.isSymbol("-") // false
String.isSymbol("!=") || String.isPunct("!=") // false
"!=".chars().every({ x.isSymbol() || x.isPunct() }) // trueAn emoji that looks like one character can hold code points that are not symbols. The red heart ❤️ ends with the variation selector U+FE0F, a mark, and a family emoji joins its people with the zero-width joiner U+200D, a format character:
String.isSymbol("❤️") // false
"❤️".chars().map({ x.category() }) // ["So", "Mn"]
String.isSymbol("👨👩👧") // falseAn emoji that a Unicode version newer than String.unicodeVersion adds is unassigned, category "Cn", not "So", so String.isSymbol is false on it until the implementation moves to that version.
String.isSymbol tests that the whole string is symbols, not that it holds one. To find out whether a string contains a symbol, test each code point:
String.isSymbol("a+b") // false
"a+b".chars().some({ x.isSymbol() }) // trueSee also
String.category · String.isPunct · String.isMark · String.isGraphic · String.isPrint · String.chars · String.graphemes
