Quick Contact

    String in JavaScript

    Introduction

    A string is a series or combination of letters, integers, special characters, and mathematical values. Strings can be produced by enclosing the string literal (i.e. string characters) either between single quotes (‘) or double quotations (“).

    Syntax of JavaScript String

    The syntax of defining a String literal is as follows:

    varstringName = “string “;

    varstringName = “string”;

    The syntax to create a String global object looks like this, where data is any object that is convertible to a string:

    varstringName = newString(data);

    JavaScript Escape Sequences

    Escape sequences are also useful for situations where you want to use characters that can’t be typed using a keyboard. Here are some other most commonly used escape sequences.

    • \n is replaced by the newline character
    • \t is replaced by the tab character
    • \r is replaced by the carriage-return character
    • \b is replaced by the backspace character
    • \\ is replaced by a single backslash (\)

    Performing Operations on Strings

    To conduct operations on string values, JavaScript provides numerous properties and functions. Only objects can technically have attributes and methods. However, in JavaScript, primitive data types can behave like objects when referenced using the property access notation (i.e. dot notation).

    JavaScript enables this by generating a temporary container object for basic data types. The JavaScript processor in the background handles this procedure automatically.

    JavaScript String Methods

    Let’s go over the JavaScript string methods and their examples.

    Methods Description
    charAt() It returns the char value found at the provided index.
    charCodeAt() It returns the Unicode value of a character at the requested index.
    concat() It allows you to combine two or more strings.
    indexOf() It returns the location of a char value in the specified string.
    lastIndexOf() It returns the location of a char value in the provided string by searching from the last position.
    search() It scans a given text for a defined regular expression and provides its place if a match is found.
    match() It examines a given string for a defined regular expression and returns that regular expression if a match is found.
    replace() It replaces the specified string with the provided string.
    substr() It is utilized to get a portion of the provided string based on the defined starting place and length.
    substring() It is used to get a portion of the provided text based on the defined index.
    slice() It is used to retrieve a portion of the specified string. It enables us to give both positive and negative indexes.
    toLowerCase() It lowercases the string that is sent to it.
    toLocaleLowerCase() It changes the provided string to lowercase letters based on the host?
    ‘s current location
    toUpperCase() It changes the string to uppercase letters.
    toLocaleUpperCase() It changes the provided text to uppercase letters based on the host?
    ‘s current location
    toString() It returns a string that represents the specific item.
    valueOf() It returns the strings object’s primitive value.
    split() It divides a string into substring arrays and returns the resulting array.
    trim() It removes the white space from the string’s left and right sides.

    Getting the Length of a String

    The length attribute gives the string’s length, which is the amount of characters it contains. This also contains the quantity of special characters, such as \t or \n.

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>Get String Length in JavaScript< /title>
    < /head>
    < body>
    < script>
    var str1 = "Ducat is Learning Platform";
    document.write(str1.length + "< br>"); // Prints 26
    
    var str2 = "Ducat is \n Learning Platform.";
    document.write(str2.length); // Prints 29, because \n is only one character
    < /script>
    < /body>
    < /html>
    

    Searching for a Pattern Inside a String

    You may use the search() function to find a specific element of text or pattern within a string.

    The search() function, like the indexOf() method, produces the index of the first match and -1 if no matches are discovered; however, unlike the indexOf() method, this method may additionally accept a regular expression as an input to give sophisticated search capabilities.

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Search Text or Pattern inside a String< /title>
    < /head>
    < body>
    < script>
    varstr = "Color red looks brighter than color blue.";
    
        // Case sensitive search
    var pos1 = str.search("color");
    document.write(pos1 + "< br>"); // 0utputs: 30
    
        // Case insensitive search using regexp
    var pos2 = str.search(/color/i);
    document.write(pos2); // 0utputs: 0
    < /script>
    < /body>
    < /html>
    

    Extracting a Fixed Number of Characters from a String

    JavaScript also provide the substr() method which is similar to slice() with a subtle difference, the second parameter specifies the number of characters to extract instead of ending index, like str.substr(startIndex, length). If length is 0 or a negative number, an empty string is returned. The following example demonstrates how it works:

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Extract Fixed Number of Characters from a String< /title>
    < /head>
    < body>
    < script>
    varstr = "The quick brown fox jumps over the lazy dog.";
    document.write(str.substr(4, 15) + "< br>"); // Prints: quick brown fox
    document.write(str.substr(-28, -19) + "< br>"); // Prints nothing
    document.write(str.substr(-28, 9) + "< br>"); // Prints: fox jumps
    document.write(str.substr(31)); // Prints: the lazy dog.
    < /script>
    < /body>
    < /html>
    

    Output

    quick brown fox
    
    fox jumps
    the lazy dog.
    

    Replacing the Contents of a String

    You can use the replace() method to replace part of a string with another string. This method takes two parameters a regular expression to match or substring to be replaced and a replacement string, like str.replace(regexp|substr, newSubstr).

    This replace() method returns a new string, it doesn’t affect the original string that will remain unchanged.

    Converting a String to Uppercase or Lowercase

    You can use the toUpperCase() method to convert a string to uppercase, like this:

    Uppercase

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Convert a String to Uppercase Characters< /title>
    < /head>
    < body>
    < script>
    varstr = "Ducat Education";
    var result = str.toUpperCase();
    document.write(result); // Prints: HELLO WORLD!
    < /script>
    < /body>
    < /html>
    

    Output

    DUCAT EDUCATION

    Lowercase

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Convert a String to Lowercase Characters< /title>
    < /head>
    < body>
    < script>
    varstr = "Ducat Education";
    var result = str.toLowerCase();
    document.write(result); 
    < /script>
    < /body>
    < /html>
    

    Output

    ducat education

    FAQS

    Q1. What is new string in JavaScript?

    The String constructor is used to create a new String object. When invoked as a function, it converts the type to a primitive string, which is generally more helpful.

    Q2. What are string functions?

    We may use string functions using Access to generate expressions that alter text in a number of ways. On a form, for example, you could wish to show only a portion of a serial number. Alternatively, you may need to connect (concatenate) two strings together, such as a last name and a first name.

    Q3. What is a string in coding?

    A string data type is utilized in various programming languages for data values that are made up of ordered sequences of characters, such as “hello world.” A string can include any visible or unseen series of characters, and characters can be repeated.

    Q4. What is array in JavaScript?

    Arrays are a subset of elements. In JavaScript, the typeof operator returns “object” for arrays. JavaScript arrays, on the other hand, are best characterized as arrays. Arrays access their “elements” using integers.

    Q5. How to declare a string in JavaScript?

    In JavaScript, we must use the var, let, or const keyword to establish variables. If it’s a string or a number, use the var, let, or const keyword to declare it. However, while declaring a string variable, users had to enclose the string in double or single quotes.

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.