SQL PHP HTML ASP JavaScript articles and free scripts to download
JavaScript Tutorials
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

 
 

Slice to collect part of a String

We can collect any part of the string by using slice function in JavaScript. Using this function we have to pass two parameters p1 and p2. Value of p1 indicates starting position of the sub string and value of p2 gives length of the sub string. The first position ( left side ) of the main string is known as 0 position 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) . Here is an example of slice function collecting the first 7 characters of the string.

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.
Further readings
Lower case text to Uppercase text
Uppercase letter to Lower case
Bold text letters using JavaScript
String Length
Character at a location using charat function
Indexof function
String Reverse function
search():Searching String inside another string
slice():Collecting part of a string using slice function
Domain and userid part of email address
Text box onBlur click event changing text case
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript