String.indexOf
func String.indexOf(s: string, t: string): int | nullString.indexOf returns the code point index of the first occurrence of t in s, or null when t does not occur.
Details
- The index counts code points, like
String.lengthandString.slice. - When
tdoes not occur ins, the result isnull, not-1. - An empty
toccurs at every index, so the result is0. - Matching is exact, code point by code point: no case folding and no normalization.
Basic examples
String.indexOf("banana", "an") // 1
String.indexOf("banana", "x") // nullCall it through . on any string:
"hello world".indexOf("o") // 4
"hello world".indexOf("w") // 6Scope
The first occurrence wins, even when a later one overlaps it:
"aaa".indexOf("aa") // 0
"abababa".indexOf("aba") // 0t may be all of s, but not longer:
"ant".indexOf("ant") // 0
"ant".indexOf("ants") // nullNothing occurs in an empty string, but the empty string occurs at the start of any string:
"".indexOf("a") // null
"ant".indexOf("") // 0
"".indexOf("") // 0The index counts code points, not bytes. Each of ï and é is one code point and two bytes:
"naïve café".indexOf("café") // 6
"🐜🐝🐞".indexOf("🐞") // 2It counts code points, not what a reader sees as characters. The family emoji is five code points, and the accented e below is two:
"👨👩👧x".indexOf("x") // 5
"e\u{301}x".indexOf("x") // 2Call it with named arguments, or in a pipe, where the string is the first argument:
String.indexOf(s: "a-b", t: "-") // 1
"k=v" | String.indexOf("=") // 1Applications
Take the text after the first dot, and return early with ?? return when there is none:
func afterFirstDot(name: string): string {
let i = name.indexOf(".") ?? return ""
name.slice(i + 1, name.length())
}
println(afterFirstDot("archive.tar.gz"))
println(afterFirstDot("README"))tar.gzFind every occurrence by searching again after each match, and stop with ?? break:
let s = "the cat and the hat"
var from = 0
let hits: Array[int] = []
while true {
let i = s.slice(from, s.length()).indexOf("the") ?? break
hits.push(from + i)
from = from + i + 1
}
println(hits)[0, 12]Advancing by one also finds overlapping matches; advance by String.length(t) to match String.count.
Narrow the result with != null to split a header line at its colon:
type Header = { name: string, value: string }
func parseHeader(line: string): Header | null {
let i = line.indexOf(": ")
if i != null {
Header{ name: line.slice(0, i), value: line.slice(i + 2, line.length()) }
} else {
null
}
}
println(parseHeader("Content-Type: text/html"))
println(parseHeader("garbage"))Header{ name: "Content-Type", value: "text/html" }
nullWhen you need both sides rather than the index, String.cut splits around the first occurrence:
"Content-Type: text/html".cut(": ") // String.Cut{ before: "Content-Type", after: "text/html" }Properties and relations
The result is null exactly when String.contains is false:
let s = "banana"
for t in ["an", "nab", "a", "x"] {
assert((s.indexOf(t) == null) == !s.contains(t))
}When t occurs, the slice of s at the index is t, and no occurrence of t starts before the index:
let s = "mississippi"
let t = "ssi"
let i = s.indexOf(t)
assert(i != null)
assert(s.slice(i, i + t.length()) == t)
assert(!s.slice(0, i + t.length() - 1).contains(t))The first index is never after the last one that String.lastIndexOf gives:
for s in ["banana", "an", "nana"] {
let first = s.indexOf("an")
let last = s.lastIndexOf("an")
assert((first == null) == (last == null))
if first != null && last != null {
assert(first <= last)
}
}On the code points of s, a one-code-point t has the same index that Array.indexOf gives on String.chars:
let s = "hello"
for c in ["h", "l", "o", "z"] {
assert(s.indexOf(c) == Array.indexOf(s.chars(), c))
}Possible issues
A missing match is null, not -1, so a test written as in JavaScript is a compile error:
if "abc".indexOf("z") >= 0 {
// error: >= needs both operands of one type, got int | null and int
println("found")
}Compare against null instead, or use String.contains when the index does not matter:
"abc".indexOf("z") != null // false
"abc".contains("z") // falseThe result is not an int until you narrow it, so arithmetic on it is a compile error:
let i = "abc".indexOf("b")
println(i + 1) // error: + needs both operands of one type, got int | null and intGive a default with ?? when one makes sense:
("abc".indexOf("b") ?? 0) + 1 // 2The index counts code points. Bytes.indexOf on the UTF-8 bytes counts bytes, and gives a different number for the same text:
String.indexOf("café!", "!") // 4
Bytes.indexOf("café!".toBytes(), "!".toBytes()) // 5The search is case-sensitive, and a precomposed é does not contain the code point e:
"Ant".indexOf("ant") // null
"Ant".toLower().indexOf("ant") // 0
"café".indexOf("e") // nullThe search does not normalize either, so the two spellings of é do not find each other. Bring both strings to one form with String.normalize first:
"cafe\u{301}".indexOf("\u{E9}") // null
"cafe\u{301}".normalize("NFC").indexOf("\u{E9}") // 3See also
String.lastIndexOf · String.contains · String.count · String.cut · String.slice · String.chars · Array.indexOf · Bytes.indexOf · ?? operator
