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 slice (copy) out a piece of an string into a new string in JavaScript

5 Answers

0 votes
var s1 = "The worlds most popular Programming Q&A";

// string.slice(start-index, end-index)

var s2 = s1.slice(4, 10); 
 
document.write(s1);
document.write("<br />");
document.write(s2);
                  
/*
run:

The worlds most popular Programming Q&A  // s1
worlds                                   // s2
 
*/

 



answered Jun 9, 2015 by avibootz
0 votes
var s1 = "The worlds most popular Programming Q&A";

// string.slice(start-index, end-index)

var s2 = s1.slice(4); 
 
document.write(s1);
document.write("<br />");
document.write(s2);
                  
/*
run:

The worlds most popular Programming Q&A // s1
worlds most popular Programming Q&A     // s2
 
*/

 



answered Jun 10, 2015 by avibootz
0 votes
var s1 = "The worlds most popular Programming Q&A";

// string.slice(start-index, end-index)

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

The worlds most popular Programming Q&A  // s1
Q&A                                      // s2
 
*/

 



answered Jun 10, 2015 by avibootz
0 votes
var s1 = "The worlds most popular Programming Q&A";

// string.slice(start-index, end-index)

var s2 = s1.slice(-15, -4); 
 
document.write(s1);
document.write("<br />");
document.write(s2);
                  
/*
run:

The worlds most popular Programming Q&A  // s1
Programming                              // s2
 
*/

 



answered Jun 10, 2015 by avibootz
0 votes
var s1 = "The worlds most popular Programming Q&A";

// string.substr(start-index, length)

var s2 = s1.substr(4, 6); 
 
document.write(s1);
document.write("<br />");
document.write(s2);
                  
/*
run:

The worlds most popular Programming Q&A // s1
worlds                                  // s2
 
*/

 



answered Jun 10, 2015 by avibootz
...