← Back to Swift Course | Chapter 14: Standard Library & Best Practices | Lesson 1 of 7

String को Manipulate करना

String manipulation का मतलब है text को slice, search, और reshape करना, जैसे एक sentence को words में काटना या letters replace करना।
Syntax
swift
import Foundation

string.split(separator: " ")
string.replacingOccurrences(of: "target", with: "replacement")
string.trimmingCharacters(in: .whitespacesAndNewlines)

Splitting a String

.split(separator:) एक string को दिए गए separator character के आस-पास पीस में तोड़ता है, substrings का एक array return करते हुए।

उदाहरण: Splitting a String

markup
// Declare a constant `sentence`, set to `"Swift is fun to learn"`
let sentence = "Swift is fun to learn"
// Declare a constant `words`, set to `sentence.split(separator: " ")`
let words = sentence.split(separator: " ")
// Print `words` to the console
print(words)
// Print "Word count: \(words.count)" to the console
print("Word count: \(words.count)")

Replacing Substrings

.replacingOccurrences(of:with:) target substring की हर match को दूसरे से replace करके एक नई string return करता है।

उदाहरण: Replacing Substrings

markup
// Import the `Foundation` module
import Foundation
// Declare a constant `text`, set to `"I like cats and cats like me"`
let text = "I like cats and cats like me"
// Declare a constant `replaced`, set to `text.replacingOccurrences(of: "cats", with: "dogs")`
let replaced = text.replacingOccurrences(of: "cats", with: "dogs")
// Print `replaced` to the console
print(replaced)

Trimming Whitespace

.trimmingCharacters(in: .whitespacesAndNewlines) leading और trailing whitespace हटाता है, जो user input साफ़ करते समय आम है।

उदाहरण: Trimming Whitespace

markup
// Import the `Foundation` module
import Foundation
// Declare a constant `messy`, set to `"   hello swift   "`
let messy = "   hello swift   "
// Declare a constant `trimmed`, set to `messy.trimmingCharacters(in: .whitespacesAndNewlines)`
let trimmed = messy.trimmingCharacters(in: .whitespacesAndNewlines)
// Print "[\(trimmed)]" to the console
print("[\(trimmed)]")
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. यह भूल जाना कि .split(separator:) Substring का एक array return करता है, String नहीं, जो कुछ APIs के लिए मायने रख सकता है जो एक पूरी String expect करती हैं।
  2. किसी string पर String.Index-based APIs या .prefix() और .suffix() जैसे convenience methods के बजाय plain integer slicing उपयोग करने की कोशिश करना।
  3. .replacingOccurrences(of:with:) उपयोग करना और यह भूल जाना कि यह original को जगह पर modify करने के बजाय एक नई string return करता है।
चैप्टर सारांश
  • .split(separator:) एक string को substrings के एक array में तोड़ता है।
  • .replacingOccurrences(of:with:) matches replaced के साथ एक नई string return करता है।
  • .prefix(n) और .suffix(n) सुरक्षित रूप से पहले या आखिरी n characters return करते हैं।
  • .trimmingCharacters(in:) किसी string के ends से अनचाहे characters, आमतौर पर whitespace, हटाता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.