What is a String?

A basic data type (sure), but actually what is it?

It is simple to say that a String is just a collection of Characters. But what is that, and how can we use a String in your program?
Read on to find out.

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • None, although it would help to understand zero-indexed arrays (guide HERE)

Terminology

Array: An ordered series of objects which are the same type

Character: A character, usually associated with a letter of the alphabet

Collection: A sequence of elements that can be traversed (as many times as you want) and can be accessed by an indexed subscript

Data types: A representation of the tyoe of data that can be processed, for example Integer or String

String: Is an NSAttributedString with additional methods for mutating the content

Strings:

A simple definition:

A string is an ordered collection of characters. One example of a collection is an Array. Essentially we take a character, and put it into an Array.

The example with a single character

A

and we treat it as an element in an Array

Image for post
Image for post

Now the character “A” is essentially in an Array.

let arr = “A”

Which can then be accessed with the first element of the Array, that is

arr[0] // A

The example with multiple characters

Therefore it adds the following characters to an Array:

let arr = “text”

Image for post
Image for post

Once again we can access the first element (at position 0), but we can also access elements (1,2, and 3)

arr[0] // t

arr[1] // e

arr[2] // x

arr[3] // t

String comparisons

Programming languages enable comparison of strings, so this means that we can compare two string such as:

let str1 = “Hello”

let str2 = “World

if (str1 == str2) { // resolves to false }

Which of course is false unless the two strings are the same.

It should be noted that case matters (that is, capital and lower case letters are different).

Uses in programming languages

Within programming languages Strings are extremely common data types and can be used to store words. However, they could also be used to store numbers that are larger than the limit forInteger numbers.

Conclusion:

Strings are useful, and you will see that comparing and manipulating Strings is extremely important in your programming journey. How this actually happens in your host programming language depends upon the implementation details of such, and perhaps is something interesting to look into another time…

Extend your knowledge

  • Strings on wikipedia (Guide HERE)

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store