String.isPunct
func String.isPunct(s: string): boolString.isPunct reports whether every code point of s is punctuation (P). It is false on "".
Details
truewhen every code point ofshas a general category in P;falseon"".- Some ASCII signs are symbols, not punctuation:
$,+,<,=,>,^,`,|,~.String.isSymboltests those.
Basic examples
String.isPunct("!") // true
String.isPunct("?!") // true
String.isPunct("Hi!") // falseCall it through .:
"...".isPunct() // trueScope
The empty string has no code points to test, and gives false:
String.isPunct("") // falseASCII punctuation: brackets, quotes, the hyphen-minus, the underscore, #, %, &, *, and @:
String.isPunct("()[]{}") // true
String.isPunct("\"'") // true
String.isPunct("-_") // true
String.isPunct("#%&*@") // true
String.isPunct(",.:;!?/\\") // trueThe ASCII math and currency signs are symbols, not punctuation:
String.isPunct("+") // false
String.isPunct("$") // false
String.isPunct("<=") // false
String.isPunct("|~") // falsePunctuation of every script counts: dashes, curly quotes, guillemets, the inverted question mark, the ellipsis, and the CJK comma and full stop:
String.isPunct("—–") // true
String.isPunct("“”‘’") // true
String.isPunct("«»") // true
String.isPunct("¿¡") // true
String.isPunct("…") // true
String.isPunct("、。") // trueThe section and pilcrow signs are punctuation; the euro, copyright, degree, and emoji are symbols:
String.isPunct("§¶") // true
String.isPunct("€") // false
String.isPunct("©") // false
String.isPunct("°") // false
String.isPunct("😀") // falseWhite space is not punctuation:
String.isPunct(" ") // false
String.isPunct("! ?") // falseString.category gives the subcategory of one code point:
"(—)!_«".chars().map(String.category) // ["Ps", "Pd", "Pe", "Po", "Pc", "Pi"]Test one code point at a time with String.chars, here in a pipe:
"a,b." | String.chars() | Array.map({ x.isPunct() }) // [false, true, false, true]Pass it to a higher-order function as a function value:
["?", "ok", "", "--", "¿"].filter(String.isPunct) // ["?", "--", "¿"]Applications
Remove the punctuation from a sentence:
let line = "Well, well: who's there?"
String.concat(line.chars().filter({ !x.isPunct() })) // "Well well whos there"Count words, ignoring tokens that are only punctuation, such as a dash between spaces:
"Wait — what? ...".fields().countWhere({ !x.isPunct() }) // 2Strip the punctuation from both ends of each word, keeping the apostrophe inside:
func strip(word: string): string {
let cs = word.chars()
var start = 0
var end = cs.length()
while start < end && cs[start].isPunct() {
start += 1
}
while end > start && cs[end - 1].isPunct() {
end -= 1
}
String.concat(cs.slice(start, end))
}
println("\"Don't,\" she said (quietly).".fields().map(strip))["Don't", "she", "said", "quietly"]Properties and relations
String.isPunct is true exactly when the string is not empty and every code point has a category that starts with P:
for s in ["", "!", "?!", "«»", "$", "a.", "—", "😀"] {
let byCategory = s != "" && s.chars().every({ x.category().startsWith("P") })
assert(String.isPunct(s) == byCategory)
}Punctuation and symbols are separate categories, so no code point is both:
for c in "!$+-.<@^_`|~«€©§".chars() {
assert(!(String.isPunct(c) && String.isSymbol(c)))
}Every ASCII printable character that is not a letter, a digit, or the space is either punctuation or a symbol:
for n in Iter.range(0x21, 0x7f) {
let c = String.fromCodePoint(n)
if c != null && !String.isLetter(c) && !String.isDigit(c) {
assert(String.isPunct(c) != String.isSymbol(c))
}
}For nonempty strings, a + b is punctuation exactly when both a and b are:
for a in ["!", "«", "x"] {
for b in ["?", "»", "y"] {
assert(String.isPunct(a + b) == (String.isPunct(a) && String.isPunct(b)))
}
}Possible issues
To accept any ASCII character that is not a letter, a digit, or white space, String.isPunct alone is not enough, because some of those characters are symbols. Combine it with String.isSymbol:
String.isPunct("+=") // false
"+=".chars().every({ x.isPunct() || x.isSymbol() }) // trueThe test runs per code point, not per grapheme. A punctuation mark followed by a combining accent looks like one punctuation character, but the accent is a mark, M, so the string fails. String.isMark tests the accent:
String.isPunct("!\u{301}") // false
String.isMark("\u{301}") // true
"!\u{301}".graphemes().length() // 1Every code point must be punctuation, so String.isPunct does not answer whether a string contains punctuation. Use Array.some for that:
String.isPunct("Hi!") // false
"Hi!".chars().some({ x.isPunct() }) // trueRemoving punctuation also removes the apostrophe and the hyphen inside words:
String.concat("don't re-read".chars().filter({ !x.isPunct() })) // "dont reread"See also
String.isSymbol · String.isLetter · String.isSpace · String.isGraphic · String.isPrint · String.category · String.chars · String.fields · Array.every · Array.some
