User Tools

Site Tools


javascript:substring

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
javascript:substring [2016/07/17 17:29] – created peterjavascript:substring [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== JavaScript - SubString ====== ====== JavaScript - SubString ======
  
-Begin the extraction at position 2, and extract the rest of the string:+Extract starting at position 2, and extract the rest of the string:
  
-<code javastring>+<code javascript>
 var str = "Hello world!"; var str = "Hello world!";
 var res = str.substring(2); var res = str.substring(2);
 </code> </code>
  
-The result of res will be:+Result:
  
-<code javastring>+<code>
 llo world! llo world!
 +</code>
 +
 +
 +
 +If "start" is greater than "end", it will swap the two arguments:
 +
 +<code javascript>
 +var str = "Hello world!";
 +var res = str.substring(4, 1);
 +</code>
 +
 +Result:
 +
 +<code>
 +ell
 +</code>
 +
 +
 +
 +If "start" is less than 0, it will start extraction from index position 0:
 +
 +<code javascript>
 +var str = "Hello world!";
 +var res = str.substring(-3);
 +</code>
 +
 +Result:
 +
 +<code>
 +Hello world!
 +</code>
 +
 +
 +
 +Extract only the first character:
 +
 +<code javascript>
 +var str = "Hello world!";
 +var res = str.substring(0, 1);
 +</code>
 +
 +Result:
 +
 +<code>
 +H
 +</code>
 +
 +
 +
 +Extract only the last character:
 +
 +<code javascript>
 +var str = "Hello world!";
 +var res = str.substring(11, 12);
 +</code>
 +
 +Result:
 +
 +<code>
 +!
 </code> </code>
  
javascript/substring.1468776576.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki