Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to use slice() to extract a section of a string and returns a new string in JavaScript

2 Answers

0 votes
// str.slice(beginSlice[, endSlice])

var s1 = "method extracts a section of a string";

var s2 = s1.slice(8, -10); // s1 length - 10
document.write(s2 + "<br />");

var s2 = s1.slice(7, -10); // s1 length - 10
document.write(s2 + "<br />");

var s2 = s1.slice(7, -8); // s1 length - 8
document.write(s2 + "<br />");

var s2 = s1.slice(7, -6); // s1 length - 6
document.write(s2 + "<br />");

var s2 = s1.slice(7, -15); // s1 length - 15
document.write(s2 + "<br />");


/*
run:

xtracts a section o
extracts a section o
extracts a section of 
extracts a section of a 
extracts a sect

*/

 



answered Aug 11, 2016 by avibootz
0 votes
// str.slice(beginSlice[, endSlice])

var s1 = "method extracts a section of a string";

var s2 = s1.slice(-1);
document.write(s2 + "<br />");

var s2 = s1.slice(-2); 
document.write(s2 + "<br />");

var s2 = s1.slice(-3); 
document.write(s2 + "<br />");

var s2 = s1.slice(-6); 
document.write(s2 + "<br />");

var s2 = s1.slice(-6, -2);
document.write(s2 + "<br />");

var s2 = s1.slice(0);
document.write(s2 + "<br />");

var s2 = s1.slice(0, -5);
document.write(s2 + "<br />");


/*
run:

g
ng
ing
string
stri
method extracts a section of a string
method extracts a section of a s

*/

 



answered Aug 11, 2016 by avibootz
...