Home

rangeOfString

rangeOfString is a method used in Cocoa and Cocoa Touch to locate a substring within an NSString. It returns an NSRange describing the location and length of the first occurrence of the specified string in the receiver. If the substring is not found, the returned range has a location of NSNotFound.

There are several overloads of the method. Common signatures include: - (NSRange)rangeOfString:(NSString *)aString; - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;

Return value: An NSRange with the location and length of the found substring. If not found, location

Usage considerations: By default the search is case-sensitive and diacritic-sensitive. To perform case-insensitive searches, include NSCaseInsensitiveSearch

Swift and bridge: In Swift, String provides range(of:) returning an optional Range<String.Index>, and NSString’s rangeOfString methods

-
(NSRange)rangeOfString:(NSString
*)aString
options:(NSStringCompareOptions)mask
range:(NSRange)searchRange.
The
aString
parameter
is
the
substring
to
locate.
The
options
parameter
(mask)
controls
aspects
of
the
search,
such
as
case
sensitivity,
diacritic
sensitivity,
or
search
direction.
The
optional
searchRange
parameter
confines
the
search
to
a
portion
of
the
receiver.
equals
NSNotFound
and
length
is
0.
The
range
can
be
used
to
extract
the
substring
or
to
determine
presence
without
creating
a
new
string.
in
the
mask;
other
options
can
adjust
handling
of
diacritics,
equality,
and
direction.
The
method
is
commonly
used
for
validation,
parsing,
and
text
processing
in
Objective-C
projects.
are
available
via
bridging
for
compatibility
with
legacy
code.