String operations in JavaScript

In this JavaScript, tutorial Let’s see some examples of String operations in JavaScript. we are going to learn the Javascript function that generates the combination of string.

We need to generate all combinations of string by selecting characters at once and then rearrange that character with other characters in such a way all combination could be generated.

Java Script code to generate all the combinations of string

Steps

  • Given string
  • Use push() and join() function
  • Print result
<!DOCTYPE html>
<html>
<head>
<title>Combination of a string</title>
</head>
<body>
  <script>
function substrings(str1)
{
var array1 = [];
  for (var x = 0, y=1; x < str1.length; x++,y++) 
  {
   array1[x]=str1.substring(x, y);
    }
var combi = [];
var temp= "";
var slent = Math.pow(2, array1.length);

for (var i = 0; i < slent ; i++)
{
    temp= "";
    for (var j=0;j<array1.length;j++) {
        if ((i & Math.pow(2,j))){ 
            temp += array1[j];
        }
    }
    if (temp !== "")
    {
        combi.push(temp);
    }
}
  alert(combi.join("\n"));
}

substrings("cat");

  </script>
</body>
</html>

Output

 

In this way, we learned the JS function that generates all combinations of strings.

JavaScript code to count total words and characters from a string.

In this, we learn how to count words from a given string by using the JavaScript function.

Steps

  • Use countword() function
  • Write string
  • Display output
<html>
<head>
    <title>Count Words</title>
</head>
<body>
<script>
function countWords(str) {
str = str.replace(/(^\s*)|(\s*$)/gi,"");
str = str.replace(/[ ]{2,}/gi," ");
str = str.replace(/\n /,"\n");
return str.split(' ').length;
}
document.write( "The Word count is- " + countWords  ("  Hello this is neha")  );
</script>
</body>
</html>

Output

In this way, we learned how to count words from a string in JS.

JS code to search a word from a string.

In this, we learn how to search a word from the given string using the javascript method.

Steps

  • Take array
  • Use JS function
  • Search word
  • Display result
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Search Word In Array</title>
        <script>

            var myArr = ["Hello", "Welcome"," to"," python"," workshop"];
            
            function search()
            {
                var txt = document.getElementById('txt').value;
                var index = myArr.indexOf(txt);
                
                if(index !== -1)
                {
                    alert('Found');
                }else{
                    alert('Not Found');
                }
                
            }
            
        </script>
        
    </head>

    <body>
       
        <input type="text" name="txt" id="txt" />
        <button onclick="search();">Search</button>
        <p id="pgh"></p>
        
        <script>
            document.getElementById('pgh').innerHTML = myArr;
        </script>
        
    </body>
</html>

Output

In this way, we learned how to search words in a string using the JS method.

JS code to split a string(Convert a sentence into an array)

In this, we learn how to split a string using javascript function.

Steps

  • Take sentence
  • Use function
  • Split sentences
<!DOCTYPE html>
<html>
<body>

<title>JavaScript Strings</title>

<p>Here we split the string</p>

<p id="demo"></p>

<script>
let str = "Just listen your mind and you will succeed in life";
const myArr = str.split(" ");
document.getElementById("demo").innerHTML = myArr; 
</script>

</body>
</html>

Output

In this way, we learned how to split strings using the JS function.

JavaScript code to remove a word from a sentence

In this, we learn how to remove a word from a sentence.

Steps

  • Take sentence
  • Use function
  • Display output
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        Remove word from Sentence.
    </title>
    <h2>Remove word from Sentence.</h2>
</head>
<body>
<script>
var set = "Welcome to new hotel in munbai".replace('to','');
console.log(set);   
</script>
</body>
</html>

Output

In this way, we learned how to remove words from sentences.