substring(): extract part of the string

From a string we can get part of the string by specifying starting and ending position of the substring.
my_string= new String("Welcome to plus2net.com");
document.write(my_string.substring(10,15));
The output of above function is plus

Note that one blank space is included in the above output at the left side.

substr() and substring()

substring() functions is different than substr function. In substr() function we used start point and length of the substring required ( not end point ).

Getting domain part of email address

We will use indexOf to get the location of the string inside another string, using this we can find out location of '@' and dot '.' within an email address. Then using these two locations we will collect domain part by using substr function.

Here is the code.
<script>
var email="userid@example.com";
var start=email.indexOf('@');
var end=email.indexOf('.');
document.write(email.substring(start+1,end));
</script>
The output is
example
If the variable end is omitted, then rest of the string is considered.
JavaScript String Reference substr() indexOf to get location of a string
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Noel

    16-09-2012

    How can split an email into parts before(userID) and after (Domain) the @ sign, and then validate to see if each of these parts are valid?

    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