পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, February 8, 2017

Swift - Characters

A character in Swift is a single character String literal, addressed by the data type character. Take a look at the following example. It uses two Character constants −
import Cocoa

let char1: Character = "A"
let char2: Character = "B"

println("Value of char1 \(char1)")
println("Value of char2 \(char2)")
When the above code is compiled and executed, it produces the following result −
Value of char1 A
Value of char2 B
If you try to store more than one character in a Character type variable or constant, then Swift will not allow that. Try to type the following example in Swift Playground and you will get an error even before compilation.
import Cocoa

// Following is wrong in Swift
let char: Character = "AB"

println("Value of char \(char)")

Empty Character Variables

It is not possible to create an empty Character variable or constant which will have an empty value. The following syntax is not possible −
import Cocoa

// Following is wrong in Swift
let char1: Character = ""
var char2: Character = ""

println("Value of char1 \(char1)")
println("Value of char2 \(char2)")

Accessing Characters from Strings

As explained while discussing Swift's Strings, String represents a collection of Character values in a specified order. So we can access individual characters from the given String by iterating over that string with a for-in loop −
import Cocoa

for ch in "Hello" {
   println(ch)
}
When the above code is compiled and executed, it produces the following result −
H
e
l
l
o

Concatenating Strings with Characters

The following example demonstrates how a Swift's Character can be concatenated with Swift's String.
import Cocoa

var varA:String = "Hello "
let varB:Character = "G"

varA.append( varB )

println("Value of varC  =  \(varA)")
When the above code is compiled and executed, it produces the following result −
Value of varC Hello G

No comments:

Post a Comment