struct
String
Initializers
Declaration
init
()
Construct the String
corresponding to the given sequence of
Unicode scalars.
Declaration
init
(
_
unicodeScalars
:
String.UnicodeScalarView
)
Create an instance containing characters
.
Declaration
init
<
S
:
SequenceType
where
Character
==
Character
>
(
_
characters
:
S
)
Create an instance representing v
in the given radix
(base).
Numerals greater than 9 are represented as roman letters,
starting with a
if uppercase
is false
or A
otherwise.
Declaration
init
<
T
:
_SignedIntegerType
>
(
_
v
:
T
,
radix
:
Int
,
uppercase
:
Bool
=
default
)
Create an instance representing v
in the given radix
(base).
Numerals greater than 9 are represented as roman letters,
starting with a
if uppercase
is false
or A
otherwise.
Declaration
init
<
T
:
_UnsignedIntegerType
>
(
_
v
:
T
,
radix
:
Int
,
uppercase
:
Bool
=
default
)
Declaration
init
(
_builtinExtendedGraphemeClusterLiteral
start
:
Builtin
.
RawPointer
,
byteSize
:
Builtin
.
Word
,
isASCII
:
Builtin
.
Int1
)
Declaration
init
(
_builtinStringLiteral
start
:
Builtin
.
RawPointer
,
byteSize
:
Builtin
.
Word
,
isASCII
:
Builtin
.
Int1
)
Declaration
init
(
_builtinUTF16StringLiteral
start
:
Builtin
.
RawPointer
,
numberOfCodeUnits
:
Builtin
.
Word
)
Declaration
init
(
_builtinUnicodeScalarLiteral
value
:
Builtin
.
Int32
)
Construct an instance that is the concatenation of count
copies
of Character(repeatedValue)
Declaration
init
(
count
:
Int
,
repeatedValue
c
:
UnicodeScalar
)
Create an instance initialized to value
.
Declaration
init
(
extendedGraphemeClusterLiteral
value
:
String
)
[Foundation] Returns a String
object initialized by using a given
format string as a template into which the remaining argument
values are substituted.
Declaration
init
(
format
:
String
,
_
arguments
:
CVarArgType
...)
[Foundation] Returns a String
object initialized by using a given
format string as a template into which the remaining argument
values are substituted according to the user’s default locale.
Declaration
init
(
format
:
String
,
arguments
: [
CVarArgType
])
[Foundation] Returns a String
object initialized by using a given
format string as a template into which the remaining argument
values are substituted according to given locale information.
Declaration
init
(
format
:
String
,
locale
:
NSLocale
?,
_
args
:
CVarArgType
...)
[Foundation] Returns a String
object initialized by using a given
format string as a template into which the remaining argument
values are substituted according to given locale information.
Declaration
init
(
format
:
String
,
locale
:
NSLocale
?,
arguments
: [
CVarArgType
])
Create an instance by concatenating the elements of strings
Declaration
init
(
stringInterpolation
strings
:
String
...)
Declaration
init
(
stringInterpolationSegment
expr
:
Bool
)
Declaration
init
(
stringInterpolationSegment
expr
:
Character
)
Declaration
init
(
stringInterpolationSegment
expr
:
Float32
)
Declaration
init
(
stringInterpolationSegment
expr
:
Float64
)
Declaration
init
(
stringInterpolationSegment
expr
:
Int
)
Declaration
init
(
stringInterpolationSegment
expr
:
Int8
)
Declaration
init
(
stringInterpolationSegment
expr
:
Int16
)
Declaration
init
(
stringInterpolationSegment
expr
:
Int32
)
Declaration
init
(
stringInterpolationSegment
expr
:
Int64
)
Declaration
init
(
stringInterpolationSegment
expr
:
String
)
Declaration
init
(
stringInterpolationSegment
expr
:
UInt
)
Declaration
init
(
stringInterpolationSegment
expr
:
UInt8
)
Declaration
init
(
stringInterpolationSegment
expr
:
UInt16
)
Declaration
init
(
stringInterpolationSegment
expr
:
UInt32
)
Declaration
init
(
stringInterpolationSegment
expr
:
UInt64
)
Declaration
init
(
stringInterpolationSegment
expr
:
UnicodeScalar
)
Create an instance containing expr
's print
representation
Declaration
init
<
T
>
(
stringInterpolationSegment
expr
:
T
)
[Foundation] Returns an initialized String
object that contains a
given number of characters from a given array of Unicode
characters.
Declaration
init
(
utf16CodeUnits
:
UnsafePointer
<
unichar
>
,
count
:
Int
)
[Foundation] Returns an initialized String
object that contains a given
number of characters from a given array of UTF-16 Code Units
Declaration
init
(
utf16CodeUnitsNoCopy
:
UnsafePointer
<
unichar
>
,
count
:
Int
,
freeWhenDone
flag
:
Bool
)
Construct the String
corresponding to the given sequence of
UTF-8 code units. If utf8
contains unpaired surrogates, the
result is nil
.
Declaration
init
?(
_
utf8
:
String.UTF8View
)
Construct the String
corresponding to the given sequence of
UTF-16 code units. If utf16
contains unpaired surrogates, the
result is nil
.
Declaration
init
?(
_
utf16
:
String.UTF16View
)
[Foundation] Produces a string containing the bytes in a given C array, interpreted according to a given encoding.
Declaration
init
?(
CString
:
UnsafePointer
<
CChar
>
,
encoding
enc
:
NSStringEncoding
)
[Foundation] Produces a string created by copying the data from a given C array of UTF8-encoded bytes.
Declaration
init
?(
UTF8String
bytes
:
UnsafePointer
<
CChar
>
)
[Foundation] Produces an initialized NSString
object equivalent to the given
bytes
interpreted in the given encoding
.
Declaration
init
?
<
S
:
SequenceType
where
UInt8
==
UInt8
>
(
bytes
:
S
,
encoding
:
NSStringEncoding
)
[Foundation] Produces an initialized String
object that contains a
given number of bytes from a given buffer of bytes interpreted
in a given encoding, and optionally frees the buffer. WARNING:
this initializer is not memory-safe!
Declaration
init
?(
bytesNoCopy
bytes
:
UnsafeMutablePointer
<
Void
>
,
length
:
Int
,
encoding
:
NSStringEncoding
,
freeWhenDone
flag
:
Bool
)
[Foundation] Produces a string created by reading data from the file at a given path interpreted using a given encoding.
Declaration
init
?(
contentsOfFile
path
:
String
,
encoding
enc
:
NSStringEncoding
,
error
:
NSErrorPointer
=
default
)
[Foundation] Produces a string created by reading data from the file at a given path and returns by reference the encoding used to interpret the file.
Declaration
init
?(
contentsOfFile
path
:
String
,
usedEncoding
:
UnsafeMutablePointer
<
NSStringEncoding
>
=
default
,
error
:
NSErrorPointer
=
default
)
[Foundation] Produces a string created by reading data from a given URL
interpreted using a given encoding. Errors are written into the
inout error
argument.
Declaration
init
?(
contentsOfURL
url
:
NSURL
,
encoding
enc
:
NSStringEncoding
,
error
:
NSErrorPointer
=
default
)
[Foundation] Produces a string created by reading data from a given URL
and returns by reference the encoding used to interpret the
data. Errors are written into the inout error
argument.
Declaration
init
?(
contentsOfURL
url
:
NSURL
,
usedEncoding
enc
:
UnsafeMutablePointer
<
NSStringEncoding
>
=
default
,
error
:
NSErrorPointer
=
default
)
Instance Variables
[Foundation] Produce a string with the first character from each word changed to the corresponding uppercase value.
Declaration
var
capitalizedString
:
String
{
get
}
A textual representation of self
, suitable for debugging.
Declaration
var
debugDescription
:
String
{
get
}
[Foundation] Returns a string made by normalizing the String
’s
contents using Form D.
Declaration
var
decomposedStringWithCanonicalMapping
:
String
{
get
}
[Foundation] Returns a string made by normalizing the String
’s
contents using Form KD.
Declaration
var
decomposedStringWithCompatibilityMapping
:
String
{
get
}
The String
's "past the end" position.
endIndex
is not a valid argument to subscript
, and is always
reachable from startIndex
by zero or more applications of
successor()
.
Declaration
var
endIndex
:
String.Index
{
get
}
[Foundation] Returns the fastest encoding to which the String
may be
converted without loss of information.
Declaration
var
fastestEncoding
:
NSStringEncoding
{
get
}
[Foundation] An unsigned integer that can be used as a hash table address.
Declaration
var
hash
:
Int
{
get
}
The hash value.
Axiom: x == y
implies x.hashValue == y.hashValue
Note: the hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.
Declaration
var
hashValue
:
Int
{
get
}
[Foundation] Returns the last path component of the String
.
Declaration
var
lastPathComponent
:
String
{
get
}
Declaration
var
lowercaseString
:
String
{
get
}
A contiguously-stored nul-terminated UTF-8 representation of
self
.
To access the underlying memory, invoke
withUnsafeBufferPointer
on the ContiguousArray
.
Declaration
var
nulTerminatedUTF8
:
ContiguousArray
<
CodeUnit
>
{
get
}
[Foundation] Returns an array of NSString objects containing, in
order, each path component of the String
.
Declaration
var
pathComponents
: [
String
] {
get
}
[Foundation] Interprets the String
as a path and returns the
String
’s extension, if any.
Declaration
var
pathExtension
:
String
{
get
}
[Foundation] Returns a string made by normalizing the String
’s
contents using Form C.
Declaration
var
precomposedStringWithCanonicalMapping
:
String
{
get
}
[Foundation] Returns a string made by normalizing the String
’s
contents using Form KC.
Declaration
var
precomposedStringWithCompatibilityMapping
:
String
{
get
}
[Foundation] Returns the smallest encoding to which the String
can
be converted without loss of information.
Declaration
var
smallestEncoding
:
NSStringEncoding
{
get
}
The position of the first Character
if the String
is
non-empty; identical to endIndex
otherwise.
Declaration
var
startIndex
:
String.Index
{
get
}
[Foundation] Returns a new string that replaces the current home
directory portion of the current path with a tilde (~
)
character.
Declaration
var
stringByAbbreviatingWithTildeInPath
:
String
{
get
}
[Foundation] Returns a new string made by deleting the last path
component from the String
, along with any final path
separator.
Declaration
var
stringByDeletingLastPathComponent
:
String
{
get
}
[Foundation] Returns a new string made by deleting the extension (if
any, and only the last) from the String
.
Declaration
var
stringByDeletingPathExtension
:
String
{
get
}
[Foundation] Returns a new string made by expanding the initial
component of the String
to its full path value.
Declaration
var
stringByExpandingTildeInPath
:
String
{
get
}
[Foundation] Returns a new string made from the String
by replacing
all percent encoded sequences with the matching UTF-8
characters.
Declaration
var
stringByRemovingPercentEncoding
:
String
? {
get
}
[Foundation] Returns a new string made from the String
by resolving
all symbolic links and standardizing path.
Declaration
var
stringByResolvingSymlinksInPath
:
String
{
get
}
[Foundation] Returns a new string made by removing extraneous path
components from the String
.
Declaration
var
stringByStandardizingPath
:
String
{
get
}
The value of self
as a collection of Unicode scalar values.
Declaration
var
unicodeScalars
:
String.UnicodeScalarView
{
get
set
}
Declaration
var
uppercaseString
:
String
{
get
}
[Foundation] Returns the number of Unicode characters in the String
.
Declaration
var
utf16Count
:
Int
{
get
}
Subscripts
Declaration
subscript
(
i
:
String.Index
) -
>
Character
{
get
}
Declaration
subscript
(
subRange
:
Range
<
String.Index
>
) -
>
String
{
get
}
Static Methods
[Foundation] Returns an Array of the encodings string objects support in the application’s environment.
Declaration
static
func
availableStringEncodings
() -
>
[
NSStringEncoding
]
[Foundation] Returns the C-string encoding assumed for any method accepting a C string as an argument.
Declaration
static
func
defaultCStringEncoding
() -
>
NSStringEncoding
Creates a new String
by copying the nul-terminated UTF-8 data
referenced by a CString
.
Returns nil
if the CString
is NULL
or if it contains ill-formed
UTF-8 code unit sequences.
Declaration
static
func
fromCString
(
cs
:
UnsafePointer
<
CChar
>
) -
>
String
?
Creates a new String
by copying the nul-terminated UTF-8 data
referenced by a CString
.
Returns nil
if the CString
is NULL
. If CString
contains
ill-formed UTF-8 code unit sequences, replaces them with replacement
characters (U+FFFD).
Declaration
static
func
fromCStringRepairingIllFormedUTF8
(
cs
:
UnsafePointer
<
CChar
>
) -
>
(
String
?,
hadError
:
Bool
)
[Foundation] Returns a human-readable string giving the name of a given encoding.
Declaration
static
func
localizedNameOfStringEncoding
(
encoding
:
NSStringEncoding
) -
>
String
[Foundation] Returns a string created by using a given format string as a template into which the remaining argument values are substituted according to the user's default locale.
Declaration
static
func
localizedStringWithFormat
(
format
:
String
,
_
arguments
:
CVarArgType
...) -
>
String
Instance Methods
[Foundation] Returns a representation of the String
as a C string
using a given encoding.
Declaration
func
cStringUsingEncoding
(
encoding
:
NSStringEncoding
) -
>
[
CChar
]?
[Foundation] Returns a Boolean value that indicates whether the
String
can be converted to a given encoding without loss of
information.
Declaration
func
canBeConvertedToEncoding
(
encoding
:
NSStringEncoding
) -
>
Bool
[Foundation] Returns a capitalized representation of the String
using the specified locale.
Declaration
func
capitalizedStringWithLocale
(
locale
:
NSLocale
?) -
>
String
[Foundation] Returns the result of invoking compare:options:
with
NSCaseInsensitiveSearch
as the only option.
Declaration
func
caseInsensitiveCompare
(
aString
:
String
) -
>
NSComparisonResult
[Foundation] Compares the string using the specified options and returns the lexical ordering for the range.
Declaration
func
compare
(
aString
:
String
,
options
mask
:
NSStringCompareOptions
=
default
,
range
:
Range
<
String.Index
>
? =
default
,
locale
:
NSLocale
? =
default
) -
>
NSComparisonResult
[Foundation] Interprets the String
as a path in the file system and
attempts to perform filename completion, returning a numeric
value that indicates whether a match was possible, and by
reference the longest path that matches the String
.
Returns the actual number of matching paths.
Declaration
func
completePathIntoString
(
_
outputName
:
UnsafeMutablePointer
<
String
>
=
default
,
caseSensitive
:
Bool
,
matchesIntoArray
:
UnsafeMutablePointer
<
[
String
]
>
=
default
,
filterTypes
: [
String
]? =
default
) -
>
Int
[Foundation] Returns an array containing substrings from the String
that have been divided by characters in a given set.
Declaration
func
componentsSeparatedByCharactersInSet
(
separator
:
NSCharacterSet
) -
>
[
String
]
[Foundation] Returns an NSData
object containing a representation of
the String
encoded using a given encoding.
Declaration
func
dataUsingEncoding
(
encoding
:
NSStringEncoding
,
allowLossyConversion
:
Bool
=
default
) -
>
NSData
?
[Foundation] Performs linguistic analysis on the specified string by enumerating the specific range of the string, providing the Block with the located tags.
Declaration
func
enumerateLinguisticTagsInRange
(
range
:
Range
<
String.Index
>
,
scheme
tagScheme
:
String
,
options
opts
:
NSLinguisticTaggerOptions
,
orthography
:
NSOrthography
?,
_
body
: (
String
,
Range
<
String.Index
>
,
Range
<
String.Index
>
,
inout
Bool
) -
>
())
[Foundation] Enumerates the substrings of the specified type in the specified range of the string.
Declaration
func
enumerateSubstringsInRange
(
range
:
Range
<
String.Index
>
,
options
opts
:
NSStringEnumerationOptions
,
_
body
: (
substring
:
String
,
substringRange
:
Range
<
String.Index
>
,
enclosingRange
:
Range
<
String.Index
>
,
inout
Bool
) -
>
())
Append the elements of newElements
to self
.
Declaration
mutating
func
extend
<
S
:
SequenceType
where
Character
==
Character
>
(
newElements
:
S
)
[Foundation] Returns a file system-specific representation of the String
.
Declaration
func
fileSystemRepresentation
() -
>
[
CChar
]
Return a generator over the Characters
in this String
.
Complexity: O(1)
Declaration
func
generate
() -
>
IndexingGenerator
<
String
>
[Foundation] Gets a given range of characters as bytes in a specified encoding.
Note: will get a maximum of min(buffer.count, maxLength)
bytes.
Declaration
func
getBytes
(
inout
buffer
: [
UInt8
],
maxLength
:
Int
,
usedLength
:
UnsafeMutablePointer
<
Int
>
,
encoding
:
NSStringEncoding
,
options
:
NSStringEncodingConversionOptions
,
range
:
Range
<
String.Index
>
,
remainingRange
:
UnsafeMutablePointer
<
Range
<
String.Index
>
>
) -
>
Bool
[Foundation] Interprets the String
as a system-independent path and
fills a buffer with a C-string in a format and encoding suitable
for use with file-system calls. Note: will store a maximum of
min(buffer.count, maxLength)
bytes.
Declaration
[Foundation] Returns by reference the beginning of the first line and the end of the last line touched by the given range.
Declaration
func
getLineStart
(
start
:
UnsafeMutablePointer
<
String.Index
>
,
end
:
UnsafeMutablePointer
<
String.Index
>
,
contentsEnd
:
UnsafeMutablePointer
<
String.Index
>
,
forRange
:
Range
<
String.Index
>
)
[Foundation] Returns by reference the beginning of the first paragraph and the end of the last paragraph touched by the given range.
Declaration
func
getParagraphStart
(
start
:
UnsafeMutablePointer
<
String.Index
>
,
end
:
UnsafeMutablePointer
<
String.Index
>
,
contentsEnd
:
UnsafeMutablePointer
<
String.Index
>
,
forRange
:
Range
<
String.Index
>
)
Insert newElement
at index i
.
Invalidates all indices with respect to self
.
Complexity: O(count(self)
).
Declaration
mutating
func
insert
(
newElement
:
Character
,
atIndex
i
:
String.Index
)
Interpose self
between every pair of consecutive elements
,
then concatenate the result. For example:
"-|-"
.
join
([
"foo"
,
"bar"
,
"baz"
])
// "foo-|-bar-|-baz"
Declaration
func
join
<
S
:
SequenceType
where
String
==
String
>
(
elements
:
S
) -
>
String
[Foundation] Returns the number of bytes required to store the
String
in a given encoding.
Declaration
func
lengthOfBytesUsingEncoding
(
encoding
:
NSStringEncoding
) -
>
Int
[Foundation] Returns the range of characters representing the line or lines containing a given range.
Declaration
func
lineRangeForRange
(
aRange
:
Range
<
String.Index
>
) -
>
Range
<
String.Index
>
[Foundation] Returns an array of linguistic tags for the specified range and requested tags within the receiving string.
Declaration
func
linguisticTagsInRange
(
range
:
Range
<
String.Index
>
,
scheme
tagScheme
:
String
,
options
opts
:
NSLinguisticTaggerOptions
=
default
,
orthography
:
NSOrthography
? =
default
,
tokenRanges
:
UnsafeMutablePointer
<
[
Range
<
String.Index
>
]
>
=
default
) -
>
[
String
]
[Foundation] Compares the string and a given string using a case-insensitive, localized, comparison.
Declaration
func
localizedCaseInsensitiveCompare
(
aString
:
String
) -
>
NSComparisonResult
[Foundation] Compares the string and a given string using a localized comparison.
Declaration
func
localizedCompare
(
aString
:
String
) -
>
NSComparisonResult
[Foundation] Compares strings as sorted by the Finder.
Declaration
func
localizedStandardCompare
(
string
:
String
) -
>
NSComparisonResult
[Foundation] Returns a version of the string with all letters converted to lowercase, taking into account the specified locale.
Declaration
func
lowercaseStringWithLocale
(
locale
:
NSLocale
) -
>
String
[Foundation] Returns the maximum number of bytes needed to store the
String
in a given encoding.
Declaration
func
maximumLengthOfBytesUsingEncoding
(
encoding
:
NSStringEncoding
) -
>
Int
[Foundation] Returns the range of characters representing the paragraph or paragraphs containing a given range.
Declaration
func
paragraphRangeForRange
(
aRange
:
Range
<
String.Index
>
) -
>
Range
<
String.Index
>
[Foundation] Parses the String
as a text representation of a
property list, returning an NSString, NSData, NSArray, or
NSDictionary object, according to the topmost element.
Declaration
func
propertyList
() -
>
AnyObject
[Foundation] Finds and returns the range in the String
of the first
character from a given character set found in a given range with
given options.
Declaration
func
rangeOfCharacterFromSet
(
aSet
:
NSCharacterSet
,
options
mask
:
NSStringCompareOptions
=
default
,
range
aRange
:
Range
<
String.Index
>
? =
default
) -
>
Range
<
String.Index
>
?
[Foundation] Returns the range in the String
of the composed
character sequence located at a given index.
Declaration
func
rangeOfComposedCharacterSequenceAtIndex
(
anIndex
:
String.Index
) -
>
Range
<
String.Index
>
[Foundation] Returns the range in the string of the composed character sequences for a given range.
Declaration
func
rangeOfComposedCharacterSequencesForRange
(
range
:
Range
<
String.Index
>
) -
>
Range
<
String.Index
>
[Foundation] Finds and returns the range of the first occurrence of a
given string within a given range of the String
, subject to
given options, using the specified locale, if any.
Declaration
func
rangeOfString
(
aString
:
String
,
options
mask
:
NSStringCompareOptions
=
default
,
range
searchRange
:
Range
<
String.Index
>
? =
default
,
locale
:
NSLocale
? =
default
) -
>
Range
<
String.Index
>
?
Remove all characters.
Invalidates all indices with respect to self
.
keepCapacity
, if true
, prevents the release of
allocated storage, which can be a useful optimization
when self
is going to be grown again.
Declaration
mutating
func
removeAll
(
keepCapacity
:
Bool
=
default
)
Remove and return the element at index i
Invalidates all indices with respect to self
.
Complexity: O(count(self)
).
Declaration
mutating
func
removeAtIndex
(
i
:
String.Index
) -
>
Character
Remove the indicated subRange
of characters
Invalidates all indices with respect to self
.
Complexity: O(count(self)
).
Declaration
mutating
func
removeRange
(
subRange
:
Range
<
String.Index
>
)
Replace the given subRange
of elements with newElements
.
Invalidates all indices with respect to self
.
Complexity: O(count(subRange)
) if subRange.endIndex
== self.endIndex
and isEmpty(newElements)
, O(N) otherwise.
Declaration
mutating
func
replaceRange
<
C
:
CollectionType
where
Character
==
Character
>
(
subRange
:
Range
<
String.Index
>
,
with
newElements
:
C
)
Reserve enough space to store n
ASCII characters.
Complexity: O(n
)
Declaration
mutating
func
reserveCapacity
(
n
:
Int
)
Insert newElements
at index i
Invalidates all indices with respect to self
.
Complexity: O(count(self) + count(newElements)
).
Declaration
mutating
func
splice
<
S
:
CollectionType
where
Character
==
Character
>
(
newElements
:
S
,
atIndex
i
:
String.Index
)
[Foundation] Returns a new string made from the String
by replacing
all characters not in the specified set with percent encoded
characters.
Declaration
func
stringByAddingPercentEncodingWithAllowedCharacters
(
allowedCharacters
:
NSCharacterSet
) -
>
String
?
[Foundation] Returns a representation of the String
using a given
encoding to determine the percent escapes necessary to convert
the String
into a legal URL string.
Declaration
func
stringByAddingPercentEscapesUsingEncoding
(
encoding
:
NSStringEncoding
) -
>
String
?
[Foundation] Returns a string made by appending to the String
a
string constructed from a given format string and the following
arguments.
Declaration
func
stringByAppendingFormat
(
format
:
String
,
_
arguments
:
CVarArgType
...) -
>
String
[Foundation] Returns a string with the given character folding options applied.
Declaration
func
stringByFoldingWithOptions
(
options
:
NSStringCompareOptions
,
locale
:
NSLocale
) -
>
String
[Foundation] Returns a new string formed from the String
by either
removing characters from the end, or by appending as many
occurrences as necessary of a given pad string.
Declaration
[Foundation] Returns a new string in which the characters in a
specified range of the String
are replaced by a given string.
Declaration
func
stringByReplacingCharactersInRange
(
range
:
Range
<
String.Index
>
,
withString
replacement
:
String
) -
>
String
[Foundation] Returns a new string in which all occurrences of a target
string in a specified range of the String
are replaced by
another given string.
Declaration
func
stringByReplacingOccurrencesOfString
(
target
:
String
,
withString
replacement
:
String
,
options
:
NSStringCompareOptions
=
default
,
range
searchRange
:
Range
<
String.Index
>
? =
default
) -
>
String
[Foundation] Returns a new string made by replacing in the String
all percent escapes with the matching characters as determined
by a given encoding.
Declaration
func
stringByReplacingPercentEscapesUsingEncoding
(
encoding
:
NSStringEncoding
) -
>
String
?
[Foundation] Returns a new string made by removing from both ends of
the String
characters contained in a given character set.
Declaration
func
stringByTrimmingCharactersInSet
(
set
:
NSCharacterSet
) -
>
String
[Foundation] Returns a new string containing the characters of the
String
from the one at a given index to the end.
Declaration
func
substringFromIndex
(
index
:
String.Index
) -
>
String
[Foundation] Returns a new string containing the characters of the
String
up to, but not including, the one at a given index.
Declaration
func
substringToIndex
(
index
:
String.Index
) -
>
String
[Foundation] Returns a string object containing the characters of the
String
that lie within a given range.
Declaration
func
substringWithRange
(
aRange
:
Range
<
String.Index
>
) -
>
String
If the string represents an integer that fits into an Int, returns the corresponding integer. This accepts strings that match the regular expression "[-+]?[0-9]+" only.
Declaration
func
toInt
() -
>
Int
?
[Foundation] Returns a version of the string with all letters converted to uppercase, taking into account the specified locale.
Declaration
func
uppercaseStringWithLocale
(
locale
:
NSLocale
) -
>
String
Invoke f
on the contents of this string, represented as
a nul-terminated array of char, ensuring that the array's
lifetime extends through the execution of f
.
Declaration
func
withCString
<
Result
>
(
f
: @
noescape
UnsafePointer
<
Int8
>
-
>
Result
) -
>
Result
Write a textual representation of self
into target
Declaration
func
writeTo
<
Target
:
OutputStreamType
>
(
inout
target
:
Target
)
An arbitrary Unicode string value.
Unicode-Correct
Swift strings are designed to be Unicode-correct. In particular, the APIs make it easy to write code that works correctly, and does not surprise end-users, regardless of where you venture in the Unicode character space. For example,
The
==
operator checks for Unicode canonical equivalence, so two different representations of the same string will always compare equal.String elements are
Characters
(extended grapheme clusters), a unit of text that is meaningful to most humans.Locale-Insensitive
The fundamental operations on Swift strings are not sensitive to locale settings. That's because, for example, the validity of a
Dictionary<String, T>
in a running program depends on a given string comparison having a single, stable result. Therefore, Swift always uses the default, un-tailored Unicode algorithms for basic string operations.Importing
Foundation
endows swift strings with the full power of theNSString
API, which allows you to choose more complex locale-sensitive operations explicitly.Value Semantics
Each string variable,
let
binding, or stored property has an independent value, so mutations to the string are not observable through its copies:Strings use Copy-on-Write so that their data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may cost
O(N)
time and space, whereN
is the length of the string's (unspecified) underlying representation,.Growth and Capacity
When a string's contiguous storage fills up, new storage must be allocated and characters must be moved to the new storage.
String
uses an exponential growth strategy that makesappend
a constant time operation when amortized over many invocations.Objective-C Bridge
String
is bridged to Objective-C asNSString
, and aString
that originated in Objective-C may store its characters in anNSString
. Since any arbitrary subclass ofNSSString
can become aString
, there are no guarantees about representation or efficiency in this case. SinceNSString
is immutable, it is just as though the storage was shared by some copy: the first in any sequence of mutating operations causes elements to be copied into unique, contiguous storage which may costO(N)
time and space, whereN
is the length of the string representation (or more, if the underlyingNSString
is has unusual performance characteristics).