स्विफ्ट वर्ण और तार (उदाहरण के साथ)

इस ट्यूटोरियल में, आप स्विफ्ट में अक्षरों और स्ट्रिंग्स के उपयोग के बारे में जानेंगे। आप अलग-अलग ऑपरेशन भी सीखेंगे जो स्ट्रिंग्स और पात्रों पर किए जा सकते हैं।

एक चरित्र क्या है?

एक चरित्र एक एकल प्रतीक (अक्षर, संख्या, आदि) है। स्विफ्ट में कैरेक्टर Characterप्रकार के होते हैं और इन्हें इस प्रकार घोषित किया जाता है:

 आज्ञा देना कुछ: चरित्र

स्विफ्ट में एक चरित्र को कैसे घोषित करें और असाइन करें?

आप वर्ण प्रकार में मान निर्दिष्ट कर सकते हैं जैसे स्ट्रिंग दोहरे उद्धरण चिह्नों का उपयोग करते हुए " "लेकिन इसमें उद्धरणों के अंदर केवल एक ही वर्ण होना चाहिए " "

यदि आपको एक से अधिक वर्ण शामिल करने की आवश्यकता है, तो आपको इसके Stringबजाय इसे परिभाषित करने की आवश्यकता है Character

उदाहरण 1: एक चरित्र की घोषणा और असाइन करना

 let someCharacter:Character = “H” let specialCharacter:Character = “@” print(someCharacter) print(specialCharacter)

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ह @

उदाहरण 2: एक से अधिक वर्ण निरुपित करना (काम नहीं करता)

लेकिन अगर आप चरित्र के अंदर दो प्रतीकों को निर्दिष्ट करने का प्रयास करते हैं

 /* This will give an error Changing the type to String will fix it. */ let failableCharacter:Character = "H@" print(failableCharacter) 

जब आप उपरोक्त कोड को चलाने का प्रयास करेंगे, तो आपको एक त्रुटि मिलेगी:

 स्ट्रिंग के प्रकार को वर्ण में परिवर्तित नहीं कर सकता।

यूनिकोड और एस्केप सीक्वेंस का उपयोग करके चरित्र का निर्माण

आप यूनिकोड का उपयोग करके for.eg इमोजी के लिए विशेष प्रकार के वर्ण भी बना सकते हैं। आप एस्केप अनुक्रम u (n) (यूनिकोड कोड बिंदु, n हेक्साडेसिमल में है) का उपयोग करके एक यूनिकोड बना सकते हैं।

उदाहरण 3: एक यूनिकोड चरित्र बनाना

 let heartShape:Character = "u(2665)" print(heartShape) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

उपरोक्त उदाहरण में, कोड से एक दिल के आकार का चरित्र बनाया गया था U+2665। यद्यपि u(2665)यह दोहरे उद्धरण चिह्नों में शामिल है, संकलक इसे Stringइसलिए नहीं मानते हैं क्योंकि हमने भागने के क्रम का उपयोग किया था u(n)। भागने का क्रम शाब्दिक में शामिल होने पर स्वयं का प्रतिनिधित्व नहीं करता है।

एक स्ट्रिंग क्या है?

एक स्ट्रिंग केवल पात्रों का एक संग्रह है। स्विफ्ट में तार Stringप्रकार के होते हैं और घोषित किए जाते हैं:

 चलो कुछ स्ट्रिंग: स्ट्रिंग

कैसे घोषित करें और स्विफ्ट में एक स्ट्रिंग आवंटित करें?

स्ट्रिंग स्ट्रिंग का उपयोग करके आप स्ट्रिंग प्रकार में मान प्रदान कर सकते हैं। एक स्ट्रिंग शाब्दिक दोहरे उद्धरण चिह्नों से घिरे पात्रों का एक संग्रह है " "

उदाहरण 4: एक स्ट्रिंग की घोषणा और असाइनमेंट

 let someString:String = "Hello, world!" let someMessage = "I love Swift." print(someString) print(someMessage)

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

नमस्ते दुनिया! मुझे स्विफ्ट बहुत पसंद है।

यहाँ, दोनों "Hello, world!"और "I love Swift."स्ट्रिंग स्ट्रिंगल्स हैं जिनका उपयोग क्रमशः स्ट्रिंग चर और कुछ मेसेजेज बनाने के लिए किया जाता है।

एक स्ट्रिंग पर संचालन

सबसे अधिक इस्तेमाल किए जाने वाले कार्यों से निपटने के लिए स्ट्रिंग में कुछ कार्य और संपत्ति बनाई गई हैं। उदाहरण के लिए: स्ट्रिंग्स में शामिल होने के लिए, इसे अपरकेस या कैपिटल में बदलें। आइए नीचे दिए गए कुछ अक्सर उपयोग किए गए कार्यों का पता लगाएं:

स्ट्रिंग तुलना

आप बस जांच कर सकते हैं कि दो तार बराबर हैं या तुलना ऑपरेटर का उपयोग नहीं कर रहे हैं (==)। ऑपरेटर वापस लौटता है trueयदि दोनों तार बराबर हैं, अन्यथा यह वापस आ जाता है false

उदाहरण 5: स्विफ्ट में स्ट्रिंग की तुलना

 let someString = "Hello, world!" let someMessage = "I love Swift." let someAnotherMessage = "Hello, world!" print(someString == someMessage) print(someString == someAnotherMessage) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 सच्चा झूठा

स्ट्रिंग संगति

अतिरिक्त ऑपरेटर के साथ (+)या कंपाउंड असाइनमेंट ऑपरेटर का उपयोग करके दो अलग-अलग स्ट्रिंग्स वैल्यू को एक साथ जोड़ा जा सकता है (+=)। आप appendविधि का उपयोग करके स्ट्रिंग में एक चरित्र / स्ट्रिंग को जोड़ सकते हैं ।

उदाहरण 6: स्विफ्ट में स्ट्रिंग का संघटन

 let helloStr = "Hello, " let worldStr = "World" var result = helloStr + worldStr print(result) result.append("!") print(result) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 हैलो, वर्ल्ड हैलो, वर्ल्ड!

उपरोक्त कार्यक्रम में हमने helloStr और worldStr को जोड़कर एक स्ट्रिंग परिणाम बनाया + ऑपरेटर का उपयोग करके। इसलिए, print(result)आउटपुट हैलो, वर्ल्ड इन द स्क्रीन।

आप appendविधि का उपयोग करके किसी भी वर्ण या स्ट्रिंग को जोड़ सकते हैं । स्ट्रिंग के अंत में result.append("!")एक !चरित्र जोड़ता है । तो, नमस्कार, विश्वprint(result) आउटपुट ! स्क्रीन पर।

उन्नत असाइनमेंट ऑपरेटर का उपयोग करते हुए स्ट्रिंग संघनन

We can also use advanced assignment operator (+=) to append string.

Example 7: String concatenation using += operator

 var helloStr = "Hello, " let worldStr = "World!" helloStr += worldStr print(helloStr) 

When you run the program, the output will be:

 Hello, World!

Notice the use of var instead of let in helloStr. If you have defined helloStr a constant using let, you cannot change it later using the += operator and eventually get an error. So, you have to define helloStr a variable.

String Interpolation

It is a simple process of evaluating a string literal that consists of variables, constants etc. Imagine you have player’s name and score stored in two constants as:

 let playerName = "Jack" let playerScore = 99

Now you need to display a message to the player as "Congratulations Jack!. Your highest score is 99." Here, you need to a way to use the values of the constants in a single string.

This can be achieved using string concatenation as:

 let congratsMessage = "Congratulations " + playerName + "!. Your highest score is " + playerScore + "." print(congratsMessage)

However, you can see this can get messy pretty soon. You have to take care of the spaces after the word Congratulations, is. Also, if you have to use more than two constants/variables, it will get unreadable.

There’s an easier way to display the message using string interpolation. Interpolation is the process to include value of a variable or constant inside string literal.

The variable or constant that should insert into the string literal is wrapped in a pair of parentheses ( ), prefixed by a backslash ().

Example 8: String interpolation in Swift

 let playerName = "Jack" let playerScore = 99 let congratsMessage = "Congratulations (playerName)!. Your highest score is (playerScore)." print(congratsMessage) 

When you run the program, the output will be:

 Congratulations Jack!. Your highest score is 99.

Some helpful built-in String functions & variables:

1. isEmpty

This function determines if a string is empty or not. It returns true if the string is empty otherwise, it returns false.

Example 9: isEmpty

 var emptyString = "" print(emptyString.isEmpty) 

When you run the program, the output will be:

 true

2. capitalized

This property is used to capitalize every word in a string.

Example 10: capitalized

 let someString = "hello, world!" print(someString.capitalized) 

When you run the program, the output will be:

 Hello, World!

3. uppercased and lowercased

The uppercased function converts string to uppercase letter and the lowercased function converts string to lowercase letter.

Example 11: uppercased() and lowercased()

 let someString = "Hello, World!" print(someString.uppercased()) print(someString.lowercased()) 

When you run the program, the output will be:

 HELLO, WORLD! hello, world!

4. Length/count

This property is used to count the total number of characters in a string.

Example 12: count

 let someString = "Hello, World!" print(someString.count) 

When you run the program, the output will be:

 13

5. hasPrefix

यह फ़ंक्शन निर्धारित करता है कि कोई स्ट्रिंग कुछ वर्णों से शुरू होती है या नहीं और बूलियन मान लौटाती है। trueयदि स्ट्रिंग उपसर्ग प्रदान किए गए मान के साथ मेल खाती है तो यह वापस आ जाती है false

उदाहरण 13: हैप्रिफ़िक्स ()

 let someString = "Hello, World!" print(someString.hasPrefix("Hell")) print(someString.hasPrefix("hell")) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 सही गलत 

6. हैफिक्स

यह फ़ंक्शन निर्धारित करता है कि कोई स्ट्रिंग कुछ वर्णों के साथ समाप्त होती है या नहीं और बूलियन मान लौटाता है। यह रिटर्न trueअगर स्ट्रिंग प्रत्यय गया मान अन्यथा रिटर्न के साथ मेल खाता है false

उदाहरण 14: hasSuffix ()

 print(someString.hasSuffix("rld!")) print(someString.hasSuffix("Rld!")) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 सही गलत 

दिलचस्प लेख...