Conteúdo do curso
JavaScript Course for Beginners: Fundamentals and Practice
Sobre a Aula

String Manipulation Methods

Welcome to Module 5! In this journey, you will master the art of manipulating strings in JavaScript, exploring powerful methods to transform, extract, and combine parts of strings.

Get ready to enhance your skills and create more dynamic and interactive programs.

Accessing Individual Characters

Accessing individual characters in a string

To understand accessing individual characters in a string, imagine a sequence of boxes, where each box contains a letter.

Each box is numbered in ascending order, starting from 0. This sequence of boxes represents the string.

When we want to access a specific character in this string, we can refer to the corresponding box using its assigned number.

For example, if we want to access the third character of the string, we refer to box number 2.

Practical example:

// Defining the string
var myString = "JavaScript";

// Accessing the character "a" in the string
var characterA = myString[1];

// Displaying the result
console.log("The character 'a' is in box 1: " + characterA);

In this example, the variable characterA will store the value ‘a’ from the string “JavaScript” using bracket notation. The message displayed in the console indicates that the character ‘a’ is in box 1. It’s important to note that counting starts from 0, so we use [1] to access the second box, where ‘a’ is located.

Importance of accessing individual characters:

Accessing individual characters of a string is useful when we need to manipulate or examine specific parts of the string.

For example, we can check if a character is a vowel, replace one character with another, or perform comparison operations between characters.

This ability is crucial for various string-related tasks, as it allows us to work with characters individually, modify them, or obtain specific information from them.

Remember to use bracket notation [] to access characters, and that the index counting starts at 0; thus, the first character is at index 0, the second at index 1, and so on.

Another example:

Now, if we want to access the character ‘o’ from the string, we can use bracket notation [] to access the character by its index. Remember that index counting starts at 0.

const string = "Hello, World!";
const character = string[4];
console.log(character); // Result: o

In the above example, we assigned the string “Hello, World!” to the variable string. Then, we used bracket notation [] to access the fifth character of the string, which has an index of 4.

The value of this character is assigned to the variable character. Finally, we print the value of character in the console, which will be “o”.

String Length

The length of a string is the number of characters it contains. To obtain the length of a string, we can use the length method.

This method returns a numeric value representing the number of characters in the string.

Let’s make an analogy with a ruler to understand better. Imagine that each character of the string is like a marking on the ruler.

By using the length method, we are counting the number of markings on the ruler, which is the number of characters in the string.

Let’s look at an example:

const string = "Hello, World!";

const length = string.length;

console.log(length); // Result: 13

In the example above, we assigned the string “Hello, World!” to the variable string. Then, we used the length method to obtain the length of the string and stored the returned value in the variable length.

Finally, we printed the value of length in the console, which will be 13, corresponding to the number of characters in the string.

Thus, we can use the length method to determine the length of a string in JavaScript by counting how many characters it has.

String Concatenation

String concatenation is the process of combining two or more strings to form a single string. In JavaScript, we can perform this operation using the addition operator (+) or the concat() method.

Let’s make an analogy with a puzzle to understand better. Imagine that each string is like a piece of a puzzle.

By concatenating, we are putting these pieces together to form a complete picture.

Let’s see some examples:

Using the addition operator (+):

const string1 = "Hello";
const string2 = "World";
const string3 = string1 + " " + string2;

console.log(string3); // Result: "Hello World"

In the example above, we have the strings “Hello” and “World.” Using the addition operator (+), we can combine the two strings and add a space between them.

The result is a new string “Hello World,” which is stored in the variable string3 and printed in the console.

We can also perform concatenation using the concat() method:

const string1 = "Hello";
const string2 = "World";
const string3 = string1.concat(" ", string2);

console.log(string3); // Result: "Hello World"

In this example, we used the concat() method on the variable string1, passing the string ” ” (a space) and the variable string2 as arguments.

The method concatenates these strings and returns the new string “Hello World,” which is stored in the variable string3 and printed in the console.

Thus, string concatenation allows us to combine different text parts to form a single string in JavaScript, like pieces of a puzzle fitting together.

Case Transformation

Case transformation refers to changing the format or writing style of a string. In JavaScript, we have three main methods to perform these transformations: toUpperCase(), toLowerCase(), and charAt().

Let’s make an analogy with a wardrobe to understand better. Imagine that strings are like clothes stored in the wardrobe.

Each transformation method is like a type of arrangement we can apply to the clothes, whether folding them, putting them on hangers, or sorting them by colors.

The toUpperCase() method transforms all characters in the string to uppercase letters. For example:

const string = "hello world";
const upperCaseString = string.toUpperCase();

console.log(upperCaseString); // Result: "HELLO WORLD"

On the other hand, the toLowerCase() method transforms all characters in the string to lowercase letters. Let’s see an example:

const string = "HELLO WORLD";
const lowerCaseString = string.toLowerCase();

console.log(lowerCaseString); // Result: "hello world"

Finally, we have the charAt() method, which allows us to get the character at a specific position in the string.

It takes the position of the desired character as an argument, starting the count from index 0. For example:

const string = "hello";
const character = string.charAt(1);

console.log(character); // Result: "e"

Thus, we can perform different case transformations on strings in JavaScript, just as arranging clothes in a wardrobe according to our taste and needs.

Substring Replacement

Substring replacement refers to replacing a specific part of a string with another. In JavaScript, we use the replace() method to perform this replacement.

Let’s use an analogy to understand better. Imagine the string as a sentence written on paper.

Substring replacement is like using a pen to erase an unwanted word in the sentence and write a new word in its place.

The replace() method takes two arguments: the first is the substring we want to replace, and the second is the new substring that will be placed in the old one. For example:

const string = "I like apples.";
const newString = string.replace("apples", "oranges");

console.log(newString); // Result: "I like oranges."

It’s important to note that the replace() method only replaces the first occurrence of the substring. If we want to replace all occurrences, we can use a regular expression with the global flag /g. Let’s see an example:

const string = "Orange is a very tasty fruit. I love orange.";
const newString = string.replace(/orange/g, "apple");

console.log(newString); // Result: "Apple is a very tasty fruit. I love apple."

In this case, we used the regular expression /orange/g as the first argument of the replace() method. The ‘g’ flag indicates that we want to replace all occurrences of the word “orange” with “apple.”

Thus, we can use the replace() method to substitute substrings in a string, just as erasing a word on paper and writing a new one in its place.

String Splitting

String splitting is the process of breaking a string into several smaller parts based on a specific separator.

In JavaScript, we use the split() method to perform this splitting.

The split() method takes the separator as an argument.

This separator can be a specific character, such as a space or a comma, or even a regular expression.

Let’s see an example to illustrate better:

const sentence = "Hello, how are you?";
const words = sentence.split(" ");

console.log(words); // Result: ["Hello,", "how", "are", "you?"]

We can use other separators, like a comma, for example:

const numbers = "1,2,3,4,5";
const arrayNumbers = numbers.split(",");

console.log(arrayNumbers); // Result: ["1", "2", "3", "4", "5"]

In this case, we used the split(",") method to divide the string of numbers into an array, using a comma as the separator.

Thus, the split() method allows us to break a string into smaller parts based on a specific separator.

String Searching

String searching is the process of finding the position of a specific sequence of characters within a larger string. In JavaScript, we use the indexOf() method to perform this search.

Let’s use an analogy to make it easier to understand. Imagine the string as a sentence written in a book.

String searching is like looking for a specific word in the book, noting on which page it is.

The indexOf() method takes the character sequence we want to find in the string as an argument.

It returns the position of the first occurrence of this sequence in the string. If the sequence is not found, the method returns -1.

Let’s see an example to illustrate better:

const sentence = "JavaScript is a powerful programming language";
const position = sentence.indexOf("JavaScript");

console.log(position); // Result: 0

In this example, we are searching for the sequence “JavaScript” in the sentence. Using the indexOf() method, we get the position where this sequence starts in the string.

In this case, the position is 0, indicating that the sequence “JavaScript” starts at the first character of the string.

If the sequence is not found, the method returns -1:

const sentence = "JavaScript is a powerful programming language";
const position = sentence.indexOf("Python");

console.log(position); // Result: -1

In this case, we are searching for the sequence “Python” in the sentence, which is not present. The indexOf() method returns -1, indicating that the sequence was not found.

Thus, the indexOf() method allows us to search for a specific sequence of characters within a string and obtain the position where it occurs. Similar to searching for a word in a book and noting the page number.

Challenge

Create a JavaScript script that receives a user’s full name as input.

Convert the full name to uppercase.

Extract the first name using indexOf and substring.

Format the first name with the last name, separating them by a space, and display the result.

Now that we’ve learned about various string manipulations in JavaScript, we’re ready to take a step further and explore a fundamental concept: objects in JavaScript.

Entrar na conversa
Rolar para cima