Open In App

How to modify a string in JavaScript ?

Last Updated : 14 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. 

Representing a String: 

1. Using double quotes or single quotes.

Javascript




<script>
  
    // With single quotes
    let string1 = 'GFG!';
    console.log(string1);
      
    // With double quotes
    let string2 = "Geeks for Geeks"
    console.log(string2);
</script>


Output:

GFG!
Geeks for Geeks

2. Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string. 

Javascript




<script>
  
    // Using double quotes inside a string
    let string1 = 'GFG is "Awesome"!';
    console.log(string1);
      
    // Using single quotes inside a string
    let string2 = "'Geeks' for 'Geeks'";
    console.log(string2);
</script>


Output:

GFG is "Awesome"!
'Geeks' for 'Geeks'

3. Strings can be used as objects by using the keyword ‘new’. 

Javascript




<script>
    let str = new String("GFG");
    console.log(str);
</script>


Output:

[String: 'GFG']

4. We can also use special characters by using a backslash(\).

Javascript




<script>
    let string1 = "Hello, \'Sam\' is using \"Geeks for Geeks\"";
    console.log(string1);
</script>


Output:

Hello, 'Sam' is using "Geeks for Geeks"

In Javascript, strings are immutable, i.e. we can’t modify them using indexes. But there are some ways, we can modify a javascript string. Let’s see various methods of doing it.

Method 1: We can use replace() method. The str.replace(A, B) method will return a string in which A in str has been replaced by B.

Javascript




<script>
    let string1 = "GFG is Awesome";
    console.log("Before Replacing : ",string1);
    let string2 = string1.replace("GFG" , "Geeks For Geeks");
    console.log("After  Replacing : ",string2);
</script>


Output:

Before Replacing :  GFG is Awesome
After  Replacing :  Geeks For Geeks is Awesome

Method 2: Using the substring method to insert a character or string at a given index. string1.substring(0,pos) + ‘a’ + string1.substring(pos) will return a string in which ‘a’ is inserted at index pos in string1.

Javascript




<script>
    let string1 = "helo";
    console.log(string1);    
    let string2 = string1.substring(0,2) 
        + 'l' + string1.substring(2);
    console.log(string2);
</script>


Output:

helo
hello

Method 3: By converting the string to a character array and then modifying the character array and then merging the character array to a string. To convert string to character arrays, we have 4 ways.

  • split(): It returns an array by splitting the string.
  • spread operator ( […string] ): It returns an array.
  • Array.from(): It returns an array from a string.
  • Object.assign([], string): It returns an array by assigning string characters to it.

To convert the character array to the string we use 

  • s.join(“”):  It returns a string by joining all items of the array.

Javascript




<script>
    let name = 'GFGWebsite';
      
    // Using split
    let arr1 = name.split('');
    arr1[0]='A';
    let string1 = arr1.join("");
    console.log(string1);
      
    // Using spread operator
    let arr2 = [...name];
    arr2[1]='B';
    arr2[2]='C';
    let string2 = arr2.join("");
    console.log(string2);
      
    // Using Array.from
    let arr3 = Array.from(name);
    arr3[1]='-';
    let string3 = arr3.join("");
    console.log(string3);
      
    // Using Object.assign
    let arr4 = Object.assign([], name);
    arr4[5]='+';
    let string4 = arr4.join("");
    console.log(string4);
</script>


Output:

AFGWebsite
GBCWebsite
G-GWebsite
GFGWe+site


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: