Difference between slice() and substring() in javascript 2013

View previous topic View next topic Go down

Difference between slice() and substring() in javascript 2013

Post by Admin on Sun Sep 15, 2013 4:02 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">Difference between slice() and substring() in javascript</br></br>slice() works like substring() works the same way for string in javascript.</br></br> var stringVar = "slice_vs_substring";</br> stringVar.slice(start, stop);</br>stringVar.substring(start, stop);</br></br><u><b>substring()</b></u>:</br></br><u><b>start==stop</b></u>: It returns an empty string.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">if(start==stop){ <br /> return ""<br />}</code></pre></br><table><tr><td><iframe src = 'http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137656371935721818&affid=kumarbhati' frameborder = 0, height=250, width = 300 > </iframe></td><td><iframe src="http://origin-img.shaadi.com/shaadi-rewards/get-banner.php?ptnr=kg9vc&banner_type=rectangles&banner_size=250x250&ad_type=iframe" width="250" height="250" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></td></tr></table></br><u><b>only start index, stringVar.substring(index)</b></u>: It returns string from [index+1] till length of string</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(3) -> return "ce_vs_substring";<br /></code></pre></br> <u><b>start > stop, stringVar.substring(10 , 0)</b></u>: It returns stringVar.substring(0, 10)</br>If start > stop, then substring will swap those 2 arguments.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(10, 0) -> return "slice_vs_s";<br /></code></pre></br> <u><b>start > LENGTH OR stop > LENGTH, stringVar.substring(0 , 40)</b></u>: It returns stringVar.substring(0, LENGTH)</br>If either argument is greater than the string's length, either argument will use the string's length.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(0 , 40) -> return "slice_vs_substring";<br /></code></pre></br> <u><b>start < 0 OR stop < 0, stringVar.substring(-10 , 4)</b></u>: It returns stringVar.substring(0, 4)</br>If either argument is less than 0 or is NaN, it is treated as if it were 0.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(-10 , 4) -> return "slic";<br /></code></pre> <hr><hr> var stringVar = "slice_vs_substring";</br> stringVar.slice(start, stop);</br>stringVar.substring(start, stop);</br></br><u><b>slice()</b></u>:</br></br><u><b>start==stop</b></u>: It returns an empty string.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">if(start==stop){ <br /> return ""<br />}</code></pre></br> <u><b>only start index, stringVar.slice(index)</b></u>: It returns string from [index+1] till length of string</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(3) -> return "ce_vs_substring";<br /></code></pre></br> <u><b>start > stop, stringVar.slice(1 , 4)</b></u>: It returns stringVar.slice(1, 4), i.e. from index position 1 till index position 3</br>If start > stop, then slice will swap those 2 arguments.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(1, 4) -> return "lic";<br /></code></pre></br> <u><b>start > LENGTH OR stop > LENGTH, stringVar.slice(0 , 40)</b></u>: It returns stringVar.substring(0, LENGTH)</br>If either argument is greater than the string's length, either argument will use the string's length.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(0 , 40) -> return "slice_vs_substring";<br /></code></pre></br> <u><b>stop < 0, stringVar.slice(0 , -4)</b></u>: It returns stringVar.substring(0, [(LENGTH -1) - Match.abs(-4))</br>If stop is negative, slice() will set stop to: (string.length – 1) – Math.abs(stop)</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(0 , -4) -> return "slice_vs_subst";<br /></code></pre></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">Difference between slice() and substring() in javascript</br></br>slice() works like substring() works the same way for string in javascript.</br></br> var stringVar = "slice_vs_substring";</br> stringVar.slice(start, stop);</br>stringVar.substring(start, stop);</br></br><u><b>substring()</b></u>:</br></br><u><b>start==stop</b></u>: It returns an empty string.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">if(start==stop){ <br /> return ""<br />}</code></pre></br><table><tr><td><iframe src = 'http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137656371935721818&affid=kumarbhati' frameborder = 0, height=250, width = 300 > </iframe></td><td><iframe src="http://origin-img.shaadi.com/shaadi-rewards/get-banner.php?ptnr=kg9vc&banner_type=rectangles&banner_size=250x250&ad_type=iframe" width="250" height="250" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></td></tr></table></br><u><b>only start index, stringVar.substring(index)</b></u>: It returns string from [index+1] till length of string</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(3) -> return "ce_vs_substring";<br /></code></pre></br> <u><b>start > stop, stringVar.substring(10 , 0)</b></u>: It returns stringVar.substring(0, 10)</br>If start > stop, then substring will swap those 2 arguments.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(10, 0) -> return "slice_vs_s";<br /></code></pre></br> <u><b>start > LENGTH OR stop > LENGTH, stringVar.substring(0 , 40)</b></u>: It returns stringVar.substring(0, LENGTH)</br>If either argument is greater than the string's length, either argument will use the string's length.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(0 , 40) -> return "slice_vs_substring";<br /></code></pre></br> <u><b>start < 0 OR stop < 0, stringVar.substring(-10 , 4)</b></u>: It returns stringVar.substring(0, 4)</br>If either argument is less than 0 or is NaN, it is treated as if it were 0.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.substring(-10 , 4) -> return "slic";<br /></code></pre> <hr><hr> var stringVar = "slice_vs_substring";</br> stringVar.slice(start, stop);</br>stringVar.substring(start, stop);</br></br><u><b>slice()</b></u>:</br></br><u><b>start==stop</b></u>: It returns an empty string.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">if(start==stop){ <br /> return ""<br />}</code></pre></br> <u><b>only start index, stringVar.slice(index)</b></u>: It returns string from [index+1] till length of string</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(3) -> return "ce_vs_substring";<br /></code></pre></br> <u><b>start > stop, stringVar.slice(1 , 4)</b></u>: It returns stringVar.slice(1, 4), i.e. from index position 1 till index position 3</br>If start > stop, then slice will swap those 2 arguments.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(1, 4) -> return "lic";<br /></code></pre></br> <u><b>start > LENGTH OR stop > LENGTH, stringVar.slice(0 , 40)</b></u>: It returns stringVar.substring(0, LENGTH)</br>If either argument is greater than the string's length, either argument will use the string's length.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(0 , 40) -> return "slice_vs_substring";<br /></code></pre></br> <u><b>stop < 0, stringVar.slice(0 , -4)</b></u>: It returns stringVar.substring(0, [(LENGTH -1) - Match.abs(-4))</br>If stop is negative, slice() will set stop to: (string.length – 1) – Math.abs(stop)</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">stringVar.slice(0 , -4) -> return "slice_vs_subst";<br /></code></pre></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum