StringProtocol
A type that can represent a string as a collection of characters.
protocol StringProtocol : BidirectionalCollection, Comparable, ExpressibleByStringInterpolation, Hashable, LosslessStringConvertible, TextOutputStream, TextOutputStreamable where Self.Element == Character, Self.Index == String.Index, Self.StringInterpolation == DefaultStringInterpolation, Self.SubSequence : StringProtocol
Do not declare new conformances to StringProtocol
. Only the String
and Substring
types in the standard library are valid conforming types.
Supertypes
protocol BidirectionalCollection
A collection that supports backward as well as forward traversal.
protocol Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol Comparable
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol LosslessStringConvertible
A type that can be represented as a string in a lossless, unambiguous way.
protocol Sequence
A type that provides sequential, iterated access to its elements.
protocol TextOutputStream
A type that can be the target of text-streaming operations.
protocol TextOutputStreamable
A source of text-streaming operations.
Requirements
associatedtype SubSequence = Substring
associatedtype UTF16View : BidirectionalCollection
associatedtype UTF8View : Collection
associatedtype UnicodeScalarView : BidirectionalCollection
init(cString: UnsafePointer<CChar>
) Creates a string from the null-terminated, UTF-8 encoded sequence of bytes at the given pointer.
init<C, Encoding>(decoding: C, as: Encoding.Type
) Creates a string from the given Unicode code units in the specified encoding.
init<Encoding>(decodingCString: UnsafePointer<Encoding.CodeUnit>, as: Encoding.Type
) Creates a string from the null-terminated sequence of bytes at the given pointer.
var unicodeScalars: Self.UnicodeScalarView
var utf16: Self.UTF16View
var utf8: Self.UTF8View
func hasPrefix(String
) -> Bool func hasSuffix(String
) -> Bool func lowercased(
) -> String func uppercased(
) -> String func withCString<Result>((UnsafePointer<CChar>) throws -> Result
) rethrows -> Result Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of UTF-8 code units.
func withCString<Result, Encoding>(encodedAs: Encoding.Type, (UnsafePointer<Encoding.CodeUnit>) throws -> Result
) rethrows -> Result Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of code units.
Citizens in Swift
Members
Available in _StringProcessing
Members
Available in RegexBuilder
where Self.SubSequence == Substring
Members
Available in Foundation
Members
var capitalized: String
A copy of the string with each word changed to its corresponding capitalized spelling.
var decomposedStringWithCanonicalMapping: String
A string created by normalizing the string’s contents using Form D.
var decomposedStringWithCompatibilityMapping: String
A string created by normalizing the string’s contents using Form KD.
var fastestEncoding: String.Encoding
The fastest encoding to which the string can be converted without loss of information.
var localizedCapitalized: String
A capitalized representation of the string that is produced using the current locale.
var localizedLowercase: String
A lowercase version of the string that is produced using the current locale.
var localizedUppercase: String
An uppercase version of the string that is produced using the current locale.
var precomposedStringWithCanonicalMapping: String
A string created by normalizing the string’s contents using Form C.
var precomposedStringWithCompatibilityMapping: String
A string created by normalizing the string’s contents using Form KC.
var removingPercentEncoding: String?
A new string made from the string by replacing all percent encoded sequences with the matching UTF-8 characters.
var smallestEncoding: String.Encoding
The smallest encoding to which the string can be converted without loss of information.
func addingPercentEncoding(withAllowedCharacters: CharacterSet
) -> String? Returns a new string created by replacing all characters in the string not in the specified set with percent encoded characters.
func appending<T>(T
) -> String func appendingFormat<T>(T, CVarArg...
) -> String Returns a string created by appending a string constructed from a given format string and the following arguments.
func cString(using: String.Encoding
) -> [CChar]? Returns a representation of the string as a C string using a given encoding.
func canBeConverted(to: String.Encoding
) -> Bool Returns a Boolean value that indicates whether the string can be converted to the specified encoding without loss of information.
func capitalized(with: Locale?
) -> String Returns a capitalized representation of the string using the specified locale.
func caseInsensitiveCompare<T>(T
) -> ComparisonResult Returns the result of invoking
compare:options:
withNSCaseInsensitiveSearch
as the only option.func commonPrefix<T>(with: T, options: String.CompareOptions
) -> String Returns a string containing characters this string and the given string have in common, starting from the beginning of each up to the first characters that aren’t equivalent.
func compare<T>(T, options: String.CompareOptions, range: Range<Self.Index>?, locale: Locale?
) -> ComparisonResult Compares the string using the specified options and returns the lexical ordering for the range.
func completePath(into: UnsafeMutablePointer<String>?, caseSensitive: Bool, matchesInto: UnsafeMutablePointer<[String]>?, filterTypes: [String]?
) -> Int 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.
func components(separatedBy: CharacterSet
) -> [String] Returns an array containing substrings from the string that have been divided by characters in the given set.
func components<T>(separatedBy: T
) -> [String] func contains<T>(T
) -> Bool Returns
true
iffother
is non-empty and contained withinself
by case-sensitive, non-literal search.func data(using: String.Encoding, allowLossyConversion: Bool
) -> Data? Returns a
Data
containing a representation of theString
encoded using a given encoding.func enumerateLines(invoking: (String, inout Bool) -> Void
) Enumerates all the lines in a string.
func enumerateSubstrings<R>(in: R, options: String.EnumerationOptions, (String?, Range<Self.Index>, Range<Self.Index>, inout Bool) -> Void
) Enumerates the substrings of the specified type in the specified range of the string.
func folding(options: String.CompareOptions, locale: Locale?
) -> String Returns a string with the given character folding options applied.
func getBytes<R>(inout [UInt8], maxLength: Int, usedLength: UnsafeMutablePointer<Int>, encoding: String.Encoding, options: String.EncodingConversionOptions, range: R, remaining: UnsafeMutablePointer<Range<Self.Index>>
) -> Bool Writes the given
range
of characters intobuffer
in a givenencoding
, without any allocations. Does not NULL-terminate.func getCString(inout [CChar], maxLength: Int, encoding: String.Encoding
) -> Bool Converts the
String
’s content to a given encoding and stores them in a buffer.func getLineStart<R>(UnsafeMutablePointer
<Self.Index>, end: UnsafeMutablePointer<Self.Index>, contentsEnd: UnsafeMutablePointer<Self.Index>, for: R) Returns by reference the beginning of the first line and the end of the last line touched by the given range.
func getParagraphStart<R>(UnsafeMutablePointer
<Self.Index>, end: UnsafeMutablePointer<Self.Index>, contentsEnd: UnsafeMutablePointer<Self.Index>, for: R) Returns by reference the beginning of the first paragraph and the end of the last paragraph touched by the given range.
func lengthOfBytes(using: String.Encoding
) -> Int Returns the number of bytes required to store the
String
in a given encoding.func lineRange<R>(for: R
) -> Range<Self.Index> Returns the range of characters representing the line or lines containing a given range.
func localizedCaseInsensitiveCompare<T>(T
) -> ComparisonResult Compares the string and the given string using a case-insensitive, localized, comparison.
func localizedCaseInsensitiveContains<T>(T
) -> Bool Returns a Boolean value indicating whether the given string is non-empty and contained within this string by case-insensitive, non-literal search, taking into account the current locale.
func localizedCompare<T>(T
) -> ComparisonResult Compares the string and the given string using a localized comparison.
func localizedStandardCompare<T>(T
) -> ComparisonResult Compares the string and the given string as sorted by the Finder.
func localizedStandardContains<T>(T
) -> Bool Returns a Boolean value indicating whether the string contains the given string, taking the current locale into account.
func localizedStandardRange<T>(of: T
) -> Range<Self.Index>? Finds and returns the range of the first occurrence of a given string, taking the current locale into account. Returns
nil
if the string was not found.func lowercased(with: Locale?
) -> String Returns a version of the string with all letters converted to lowercase, taking into account the specified locale.
func maximumLengthOfBytes(using: String.Encoding
) -> Int Returns the maximum number of bytes needed to store the
String
in a given encoding.func padding<T>(toLength: Int, withPad: T, startingAt: Int
) -> String 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.func range<T>(of: T, options: String.CompareOptions, range: Range<Self.Index>?, locale: Locale?
) -> Range<Self.Index>? 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.func rangeOfCharacter(from: CharacterSet, options: String.CompareOptions, range: Range<Self.Index>?
) -> Range<Self.Index>? 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.func rangeOfComposedCharacterSequence(at: Self.Index
) -> Range<Self.Index> Returns the range in the
String
of the composed character sequence located at a given index.func rangeOfComposedCharacterSequences<R>(for: R
) -> Range<Self.Index> Returns the range in the string of the composed character sequences for a given range.
func replacingCharacters<T, R>(in: R, with: T
) -> String Returns a new string in which the characters in a specified range of the
String
are replaced by a given string.func replacingOccurrences<Target, Replacement>(of: Target, with: Replacement, options: String.CompareOptions, range: Range<Self.Index>?
) -> String 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.
func trimmingCharacters(in: CharacterSet
) -> String Returns a new string made by removing from both ends of the
String
characters contained in a given character set.func uppercased(with: Locale?
) -> String Returns a version of the string with all letters converted to uppercase, taking into account the specified locale.
func write(to: URL, atomically: Bool, encoding: String.Encoding
) throws Writes the contents of the
String
to the URL specified by url using the specified encoding.func write<T>(toFile: T, atomically: Bool, encoding: String.Encoding
) throws Writes the contents of the
String
to a file at a given path using a given encoding.func substring(from: Self.Index
) -> String Returns a new string containing the characters of the
String
from the one at a given index to the end.func substring(to: Self.Index
) -> String Returns a new string containing the characters of the
String
up to, but not including, the one at a given index.func substring(with: Range<Self.Index>
) -> String Returns a string object containing the characters of the
String
that lie within a given range.