← Back to Swift Course | Chapter 2: Variables & Types | Lesson 7 of 8

Strings और Characters

एक string text का एक टुकड़ा है, जैसे एक word या sentence, और एक character उस text से बिल्कुल एक single letter या symbol है।
Syntax
swift
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

markup
// 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)")

Concatenation and Interpolation

Strings को + operator से जोड़ा जा सकता है, या \(expression) के साथ interpolation उपयोग करके दूसरी strings में embed किया जा सकता है।

उदाहरण: Concatenation and Interpolation

markup
// 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")

Common String Properties and Methods

Strings आम text operations के लिए .count, .isEmpty, .uppercased(), और .lowercased() जैसी उपयोगी properties expose करती हैं।

उदाहरण: Common String Properties and Methods

markup
// 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)")

Iterating Over Characters

चूंकि strings को integer से index नहीं किया जा सकता, हर character visit करने का idiomatic तरीका string को सीधे एक for-in loop में iterate करना है।

Note: यह Unicode characters को सही से handle करता है जो कई underlying scalars से बने हो सकते हैं।

उदाहरण: Iterating Over Characters

markup
// 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)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक String को एक integer से index करने की कोशिश करना जैसे str[0]; Swift strings String.Index उपयोग करती हैं Unicode के कारण, plain integer offsets नहीं।
  2. एक loop में एक बड़ी string बार-बार बनाने के लिए + उपयोग करना, जो += से append करने या एक array बनाकर join करने से कम efficient है।
  3. यह भूल जाना कि एक Character एक multi-scalar grapheme cluster (जैसे एक modifier वाला emoji) represent कर सकता है, सिर्फ एक single simple letter नहीं।
चैप्टर सारांश
  • String text represent करता है, और Character एक single grapheme cluster (visible character) represent करता है।
  • Strings को plain integers से index नहीं किया जा सकता; इसके बजाय .first, .last, या String.Index navigation उपयोग करें।
  • 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.