Strings और Characters
In this page:
let name: String = "text"
let letter: Character = "c"
let joined = first + " " + second
let message = "Hi \(name)"
name.count
name.uppercased()
Declaring Strings and Characters
एक String text का एक sequence रखता है, जबकि एक Character बिल्कुल एक visible character रखता है, अक्सर text को एक समय में एक symbol process करते समय उपयोग होता है।
उदाहरण: Declaring Strings and Characters
// Declare a constant `greeting` (a String), set to `"Hello"`
let greeting: String = "Hello"
// Declare a constant `firstLetter` (a Character), set to `"H"`
let firstLetter: Character = "H"
// Print "\(greeting) starts with \(firstLetter)" to the console
print("\(greeting) starts with \(firstLetter)")
Login to try C/C++/Java/PHP code in the editor
Concatenation and Interpolation
Strings को + operator से जोड़ा जा सकता है, या \(expression) के साथ interpolation उपयोग करके दूसरी strings में embed किया जा सकता है।
उदाहरण: Concatenation and Interpolation
// Declare a constant `first`, set to `"Swift"`
let first = "Swift"
// Declare a constant `second`, set to `"Lang"`
let second = "Lang"
// Declare a constant `combined`, set to `first + " " + second`
let combined = first + " " + second
// Print `combined` to the console
print(combined)
// Print "Combined has \(combined.count) characters" to the console
print("Combined has \(combined.count) characters")
Login to try C/C++/Java/PHP code in the editor
Common String Properties and Methods
Strings आम text operations के लिए .count, .isEmpty, .uppercased(), और .lowercased() जैसी उपयोगी properties expose करती हैं।
उदाहरण: Common String Properties and Methods
// Declare a constant `message`, set to `"Hello, Swift!"`
let message = "Hello, Swift!"
// Print "Uppercase: \(message.uppercased())" to the console
print("Uppercase: \(message.uppercased())")
// Print "Length: \(message.count)" to the console
print("Length: \(message.count)")
// Print "Is empty: \(message.isEmpty)" to the console
print("Is empty: \(message.isEmpty)")
Login to try C/C++/Java/PHP code in the editor
Iterating Over Characters
चूंकि strings को integer से index नहीं किया जा सकता, हर character visit करने का idiomatic तरीका string को सीधे एक for-in loop में iterate करना है।
उदाहरण: Iterating Over Characters
// Declare a constant `word`, set to `"Swift"`
let word = "Swift"
// Loop over `word`, binding each item to `letter`
for letter in word {
// Print `letter` to the console
print(letter)
}
Login to try C/C++/Java/PHP code in the editor
- एक
Stringको एक integer से index करने की कोशिश करना जैसेstr[0]; Swift stringsString.Indexउपयोग करती हैं Unicode के कारण, plain integer offsets नहीं। - एक loop में एक बड़ी string बार-बार बनाने के लिए
+उपयोग करना, जो+=से append करने या एक array बनाकर join करने से कम efficient है। - यह भूल जाना कि एक
Characterएक multi-scalar grapheme cluster (जैसे एक modifier वाला emoji) represent कर सकता है, सिर्फ एक single simple letter नहीं।
Stringtext represent करता है, औरCharacterएक single grapheme cluster (visible character) represent करता है।- Strings को plain integers से index नहीं किया जा सकता; इसके बजाय
.first,.last, याString.Indexnavigation उपयोग करें। - String interpolation (
\(value)) और concatenation (+) दोनों नई strings बनाते हैं। - Swift strings पूरी तरह Unicode-correct हैं, यही कारण है कि simple integer indexing allowed नहीं है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: