How Do I Slice String Using Slice method in JQuery

admin_img Posted By Bajarangi soft , Posted On 18-01-2021

In Jquery The slice() method extracts parts of a string and returns the extracted parts in a new string.Use the start and end parameters to specify the part of the string you want to extract.The first character has the position 0, the second has position 1.

How Do I Slice String Using Slice method in JQuery

Syntax

string.slice(start, end)
 
Parameter Description
start Required. The position where to begin the extraction.from position 0
end Optional. 

lets start,
Step 1:Create index.php file and implement below html code in it.
<h4>Hello world!</h4>
<h4 class="result" id="result"></h4>
<br><br>
<button class="btn btn-success" id="click">Slice</button>


Step 2:Now implement code to slice the word pon click button in jquery.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $("#click").click(function () {
            var str = "Hello world!";
            var res = str.slice(0, 5);
            $('#result').append(res);
        });
    });
</script>

Step 3:Complete Code For Doing Slice String Using Slice method in JQuery.
<!doctype html>
<html lang="en">
<head>
    <title>How Do I Slice String Using Slice method in JQuery</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <style>
        body{
            background: lightcoral;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="text-center">
        <br> <br> <br> <br> <br> <br>
        <h2>How Do I Slice String Using Slice method in JQuery</h2>
    </div>
    <br> <br> <br> <br> <br> <br>
    <div class="well">
        <h4>Hello world!</h4>
        <h4 class="result" id="result"></h4>
        <br><br>
        <button class="btn btn-success" id="click">Slice</button>
    </div>
</div>
</body>
</html>
<script>
    $(document).ready(function () {
        $("#click").click(function () {
            var str = "Hello world!";
            var res = str.slice(0, 5);
            $('#result').append(res);
        });
    });
</script>







 

Related Post