String.count
func String.count(s: string, t: string): intString.count returns the number of non-overlapping occurrences of t in s. It panics if t is empty.
Basic examples
Count the occurrences of a substring:
String.count("cheese", "e") // 3
String.count("banana", "na") // 2
String.count("banana", "kiwi") // 0Call it through . on any string:
let path = "/usr/local/bin"
path.count("/") // 3Scope
Occurrences do not overlap, so the characters of one match are not part of another:
"aaaa".count("aa") // 2
"banana".count("ana") // 1An empty string holds no occurrences, a t longer than s none either, and s itself one:
"".count("a") // 0
"ab".count("abc") // 0
"five".count("five") // 1t may be any string, an emoji or an escape such as \n included:
"🐜🐜🐜".count("🐜") // 3
"a\nb\nc\n".count("\n") // 3In a pipeline, the string is the first argument:
"2026-11-08" | String.count("-") // 2Applications
Count the lines of a text that ends each line with \n:
let text = "one\ntwo\nthree\n"
println("${text.count("\n")} lines")3 linesA last line without \n is not counted; String.lines counts it:
"one\ntwo".count("\n") // 1
"one\ntwo".lines().length() // 2Check that the brackets of an expression balance in number:
func sameCount(s: string): bool {
s.count("(") == s.count(")")
}
sameCount("(1 + 2) * (3 - 4)") // true
sameCount("f(g(x)") // falseCount a word in any case by folding both sides first:
let text = "Ant, ANT, ant."
text.foldCase().count("ant".foldCase()) // 3Properties and relations
The count is above 0 exactly when String.contains holds:
for t in ["e", "ee", "x"] {
assert(("cheese".count(t) > 0) == "cheese".contains(t))
}Counting a single code point gives the same as Array.countWhere over String.chars:
let s = "mississippi"
assert(s.count("s") == s.chars().countWhere({ x == "s" }))Splitting at t gives one piece more than the count:
for t in ["a", "an", "x"] {
assert("banana".split(t).length() == "banana".count(t) + 1)
}Possible issues
An empty t panics:
"abc".count("") // panic: String.count needs a substring that is not emptyTo count code points, use String.length; to count what a reader sees as characters, s.graphemes().length():
"e\u{301}".length() // 2
"e\u{301}".graphemes().length() // 1String.count skips an occurrence that overlaps one it counts, so "abababa" holds three "aba" but counts two. To count every position where t starts, test each one:
func countOverlapping(s: string, t: string): int {
var n = 0
for i in Iter.range(0, s.length()) {
if s.slice(i, s.length()).startsWith(t) {
n += 1
}
}
n
}
String.count("abababa", "aba") // 2
countOverlapping("abababa", "aba") // 3String.count matches substrings, not words, so "the" is found inside "other" and "then". To count whole words, split the text into fields first:
let text = "other then the"
text.count("the") // 3
text.fields().countWhere({ x == "the" }) // 1The match is case-sensitive:
"Hello hello".count("hello") // 1String.count does not normalize. A precomposed "é" does not contain the code point "e", while "e" followed by a combining accent does:
"café".count("e") // 0
"cafe\u{301}".count("e") // 1It counts code point sequences, not graphemes, so a part of an emoji joined from several people is found:
"👨👩👧".count("👩") // 1
"👨👩👧".graphemes().length() // 1See also
String.contains · String.indexOf · String.lastIndexOf · String.split · String.replace · String.fields · String.lines · String.length · String.graphemes · String.foldCase · Array.countWhere
