AlgoDaily

What are Strings and String Operations?

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Introduction

In this lesson, we will discuss one of the basic types in programming languages, with a focus on the following key points: 1. What are `strings` and common string operations? 2. Working with strings in JavaScript.

For the Python version of this lesson, please click here.

In the previous lessons, we learnt about variables and their usage. In programming languages, these variables can be assigned values of different types. These include integers, decimal numbers, sequence of alphabetical letters, booleans (true or false) among several others. Programming languages not only offer these data types, but also allow programmers to perform various operations on them. In this lesson, we will focus on the data type called string, and the operations that are defined on strings.

Strings

Strings

Most programming languages have a data type called a string, which denotes a sequence of characters, such as Hello World!. A string can contain any characters, be it a number, alphabet, or a special symbol. In most programming languages, strings are declared by enclosing the characters within single ('') or double quotation marks (""). These strings can be assigned to variables, which can then be used for further manipulation using string properties and methods.

Strings can also be defined as a sequence which are made up of many unicode characters, or in other words, they are an array of characters. Each character in a string can be accessed, and different programming languages provide different methods to perform this operation, as we'll see below.

Now let's have a closer look at strings in JavaScript.

Strings in JavaScript

Strings in JavaScript

As mentioned above, strings in most programming languages can be declared by single or double quotation marks, and so is the case with JavaScript. Following code block has few examples.

```js
var book = "The Great Gatsby";
var author = "F. Scott Fitzgerald";
```

Indexing of Strings

Indexing of Strings

Before we move to string operations, an important concept to discuss regarding strings is indexing. It facilitates the fast lookup of characters within a string by specifying character positions. Suppose we wanted to access the character r from the string in code block below. The position of r in the string is 2. This is because the indices, or the positions in a string start from 0, and not 1, as illustrated below.

var name = "Chris"

indexing

True or False

Does the following code block declare a valid string?

var country = "India";

True or False

Strings cannot have capital letters.

Operations on Strings

Operations on Strings

In programming languages, operations are defined on different data types, including strings. These operations could be regarding comparison of strings, combination, removal, or addition of characters/strings. Let's work with them in detail in the following sections.

Operations on Strings in JavaScript

String Operations in JavaScript

First string operation to discuss is concatenation. This operation allows us to combine multiple strings together. ```js var book = "The Great Gatsby" var author = "F. Scott Fitzgerald"

var sentence = book + "was written by " + author

console.log(sentence) //prints "The Great Gatsby was written by F. Scott Fitzgerald" ```

Other string operations in JavaScript are defined through string properties. They modify the strings according to the specified operation. Let's see some examples.

  • length property of string gives the length of the string.
  • indexOf property gives the index of the first occurence of the character or a substring in the string.
  • toLowerCase() converts the string to lowercase.
var book = "The Great Gatsby"
var author = "F. Scott Fitzgerald"

console.log(book.length) //prints 16
console.log(author.indexOf("z")) //prints 12
console.log(author.indexOf("Scott")) //prints 3
console.log(author.toLowerCase()) //prints "f. scott fitzgerald"

A comprehensive list of string properties is provided here.

Multiple Choice

The output to the following code block will be,

var name = "Sara"
var age = "12"
console.log(name+age)

Fill In

The output of the following code will be ___.

var animal = "Giraffe";
console.log(animal.indexOf("f"));

Properties of Strings

While working with strings, there are some important properties that need to be taken care of,

  • Strings are immutable. This means that they cannot be reassigned characters. For example, in the example below, we will get an error of item assignment as reassignment of characters is not allowed.
var name = "Lee";
name[0] = "T";
console.log(name); // 'Lee'
  • To include single quotation inside a string, we need to use double quotation marks to declare them. We cannot use single quotation marks to declare the string if we want to use single quotation marks inside the string.

Summary

Summary

In this lesson, we discussed about strings and various string operations that can be performed on them. String operations, as we've seen, are helpful in modifying strings at any time during the program rather than creating the entire string again and again. It also tells us about important properties of the already declared string variables.

One Pager Cheat Sheet

One Pager Cheat Sheet

  • In this lesson, we will explore basic types of programming languages, focusing on the string type and operations defined on strings.
  • Strings in programming languages are a data type which is a sequence of characters that can be assigned to variables, accessed and manipulated using various properties and methods in order to perform various operations.
  • Strings in JavaScript can be declared using either single or double quotation marks, as demonstrated in the following code block.
  • Indexing is an important concept when manipulating strings, as they are indexed starting from 0 and not 1.
  • Yes, the code block is valid and declares a string with a value of "India".
  • Strings in most programming languages can contain both lowercase and uppercase letters, and case is important in the output of the code.
  • String operations are defined on different data types, including strings, to perform comparison, combination, removal, and addition of characters/strings.
  • In JavaScript, strings can be manipulated through concatenation, properties such as length, indexOf and toLowerCase(), as outlined in the documentation provided by the Mozilla Developer Network.
  • The code block combines two strings using string concatenation, resulting in Sara12 as the output.
  • The code will output the index position of the letter f in the animal variable, which is 4.
  • Strings are immutable and must be declared with double quotation marks to include single quotation marks inside.
  • We learned about string operations and their benefits, which can help us modify strings without having to recreate them.
Open interactive view →