Object String and its methods. JavaScript code examples open

Object String and its methods. JavaScript code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: ES6

We can create strings in different ways:

    //directly assign to a variable or string constant
    const name = "Alexander";

    //create a string using the constructor String
    const name = new String("Alexander");

The String object has a large set of properties and methods with which we can manipulate strings.

lenght – specifies the length of the string

    const name = "Alexander";
    console.log(hello.length);//8

repeat – repeats a string

The repeat() method allows you to create a string by repeating another string multiple times. The number of repetitions is passed to as an argument.

    const name = "Alexander";
    console.log(name.repeat(2));   //AlexanderAlexander

Methods for searching in a string

indexOf() – search for the first substring

To search for some substring in a string, use the indexOf() methods (index of the first occurrence of the substring)

    const hello = "Hello, my name Alexander";
    const firststring = hello.indexOf("name");
    console.log(firststring)//12
As the second parameter, we can set the character from which to search for a substring in the string.
If the substring is not found, the number -1 is returned.

lastIndexOf() – looking for the last substring

Similar to the indexOf() method, but with the difference that this method looks for the index of the last occurrence of a substring.

    const hello = "Hello, my name Alexander. Second name - Arthur";
    const lastststring = hello.lastIndexOf("name");
    console.log(firststring)//34

includes – returns true if the string contains a specific substring

    const hello = "Hello, my name Alexander";

    console.log(hello.includes("name")); // true
    console.log(hello.includes("Second")); // false
As the second parameter, we can set the character from which to search for a substring in the string

Cut substring from string

substring – cut the string by index

The substring() method takes two parameters:

  1. The index of the character in the string, starting from which the string should be trimmed.
  2. The index to which the string should be cut. Optional parameter – if it is not specified, then the rest of the string is truncated.
    const hello = "Hello, my name Alexander";
    const world = hello.substring(9, 10);

    console.log(world); //my

slice – cuts out part of a substring

The slice method also allows you to get some part of a string from it. It takes two parameters:

  1. Сharacter index in the string, starting from which the string should be trimmed.
  2. The index to which the string should be trimmed. If it is not specified, then the rest of the string is truncated.
    const hello = "Hello, my name Alexander";
    const world = hello.slice(9, 10);
    const world2 = hello.slice(9,);

    console.log(world); //my
    console.log(world2); //my name Alexander

Substring() and slice() are very similar methods, but there are some differences between them:

  • In slice(), the start index must be less than the end index;
  • In substring(), if the start index is greater than the end index, then they are swapped.
  • slice() allows negative indexes. A negative index indicates the character index relative to the end of the string.
  • substring() doesn’t support negative indexes

Changing the case of a string

toLowerCase – converts a string to lower case

    const hello = "Hello, my name Alexander";
    console.log(hello.toLowerCase()); //hello, my name alexander

toUpperCase – converts string to uppercase

    const hello = "Hello, my name Alexander";
    console.log(hello.toUpperCase()); //HELLO MY NAME ALEXANDER

Getting a character by index

charAt – returns character by index

    const hello = "Hello, my name Alexander";
    console.log(hello.charAt(2)); //l

charCodeAt – returns the numeric character code by index

    const hello = "Hello, my name Alexander";
    console.log(hello.charCodeAt(2)); //108

trim – removes spaces

The trim() method is used to remove spaces:

    const hello = " Hello, my name Alexander ";
    console.log(hello.trim()); //"Hello, my name Alexander"

Additionally, there are a number of methods that remove spaces from a specific side of a string:

  • trimStart – removes a space from the beginning of a string
  • trimEnd – removes a space from the end of a string
  • trimLeft – removes a space from the left side of a string
  • trimRight – removes a space from the right side of a string

concat – concatenates two strings

    let hello = "Hello ";
    let world = "world";
    hello = hello.concat(world);
    console.log(hello); // Hello world

Replacing a value in a string

replace – replaces the first match of one substring with another

The first parameter of the method specifies which substring to replace, and the second parameter to which substring to replace.

    let hello = "Hello world";

    goodbye = hello.replace("Hello", "Goodbye");
    console.log(goodbye); //Goodbye world
This method only replaces the first occurrence of the substring.

replaceAll – replaces all matches of one substring with another

    let carsonroad = "Bugatti, audi,  Mercedes,  Renault, Dacia. another Mercedes, BMW.";
    carsonroad = carsonroad.replaceAll("Mercedes", "Mercedes-Benz");
    console.log(carsonroad); //Bugatti, audi,  Mercedes-Benz,  Renault, Dacia. another Mercedes-Benz, BMW.

split – splits a string into an array at a certain separator

A string is used as a separator, which is passed as a parameter to the method.

Let’s look at an example:

    const message = "Hello my dear friend!";
    const messageParts = message.split(" ");//space character
    console.log(messageParts);  // ["Hello", "my", "dear", "friend"]

Checking the beginning and end of a string

startsWith – checks if a string starts with a substring

This method returns true if it starts or false if not.

    const hello = "Hello my dear friend!";
    console.log(hello.startsWith("Hello"));// true
    console.log(hello.startsWith("Bye"));// false

endsWith – checks if a string ends with a substring

Returns true if the string ends with the specified substring.

    const hello = "Hello my dear friend!";
    console.log(hello.endsWith("friend!"));// true
    console.log(hello.endsWith("dear"));// false
Register has a role!

As the second parameter, you can pass the index of the second string from which the search begins:

    console.log(hello.startsWith("Hello", 7));//false 

Filling in a string

padStart – allows you to stretch the string on the left

The method will expand the string by a certain number of characters and fill the string on the left with spaces or given characters.

    let hello = "hello".padStart(8);//8 characters in string
    console.log(hello); // the method will add the missing 3 characters "   hello"

As the second parameter, we can pass to the methods a value that needs to be added to the string instead of spaces:

    let hello = "hello".padStart(17, "JavaScript, ");  // "JavaScript, hello"
If the added number of characters is greater than the added string, then the added string is repeated.

padEnd – allows you to stretch the line to the string

Similarly, padStart will expand the string by a certain number of characters and pad the string to the right with spaces or the given symbols.

    let hello = "hello".padEnd(8);//8 characters in the string
    console.log(hello); // method to add missing 3 characters "hello   "
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation