Slice to collect part of a String

<script type="text/javascript">
var my_str="Welcome to www.plus2net.com"
document.write(my_str.slice(0,7)) // Welcome
document.write("<br>"+my_str.slice(10)) // www.plus2net.com
document.write("<br>"+my_str.slice(10,14)) // www
document.write("<br>"+my_str.slice(-1)) // m
document.write("<br>"+my_str.slice(-4)) // .com
document.write("<br>"+my_str.slice(-12,-4)) // plus2net
</script>
We can collect any part of the string by using slice function in JavaScript.

start : required, position as number ( first char is 0 position )
end : optional, position of end of the string

Say for example we want sub string of length 10 characters from left side of the main string so here we have to use slice(0,10) .
var my_str="Welcome to www.plus2net.com";
var my_slice=my_str.slice(0,7);
document.write(my_slice);
The above code will display Welcome. We can start the slice from by living last 7 characters of the string by using negative number as p2. Let us remove last 7 characters of the string by using negative number as -7. Here is the code.
var my_str="Welcome to www.plus2net.com"
var my_slice=my_str.slice(0,-7);
document.write(my_slice);
The output of the above code will be Welcome to www.plus2 , you can note that the last 7 characters of the main string is removed ( sliced ).

Now let us try to collect userid and domain part of an email address by using slice and indexof function.
collect userid and domain part of an email address by slice indexof function

JavaScript String Reference Uppercase to lower case by toLowerCase() method
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer