<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>substring &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/substring/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Fri, 17 Jun 2022 21:43:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to show first n characters in JavaScript or TypeScript string</title>
		<link>https://codepills.com/how-to-show-first-n-characters-in-javascript-or-typescript-string/</link>
					<comments>https://codepills.com/how-to-show-first-n-characters-in-javascript-or-typescript-string/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Wed, 09 Dec 2020 12:57:38 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[substring]]></category>
		<category><![CDATA[TypeScript]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1089</guid>

					<description><![CDATA[This article will show you three different ways how to obtain substring from a string in JavaScript or TypeScript <a href="https://codepills.com/how-to-show-first-n-characters-in-javascript-or-typescript-string/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>To get a substring from a string in JavaScript or TypeScript, you have three different methods to call as an option. Besides TypeScript compiling to JavaScript, the method&#8217;s names a behaviour stay the same.</p>
<p><span id="more-1089"></span></p>
<h2>What is the difference between slice(), substr() and substring() string methods?</h2>
<p>The <code>slice()</code> method extracts parts of a string and returns the extracted parts in a <u>new string</u>. <code>slice()</code> does not change original string.</p>
<p><b>Syntax</b></p>
<p><code>string.slice(start_position, end_position_excluded)</code></p>
<p>The <code>substr()</code> method extracts parts of a string, beginning at the character at the specified position, and returns the limited number of characters. <code>substr()</code> does not change original string.</p>
<p><b>Syntax</b></p>
<p><code>string.substr(start_position, length)</code></p>
<p>The <code>substring()</code> method extracts parts of a string and returns the extracted parts in a <u>new string</u>. <code>substring()</code> does not change original string.</p>
<p><b>Syntax</b></p>
<p><code>string.substring(start_position, end_position_excluded)</code></p>
<p>As a conclusion we can say that there is a difference between <code>slice()</code> and <code>substr()</code>, while <code>substring()</code> is basically a copy of <code>slice()</code>.</p>
<p>Let&#8217;s take a better look at the differences between the individual methods.</p>
<h2>Slice()</h2>
<pre><code class="language-typescript">var string = "1234567890"
var substr=string.slice(4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">567890</code></pre>
<p><code>substr(4)</code> will remain everything after first 4 characters.</p>
<pre><code class="language-typescript">var string = "1234567890"
var substr = string.slice(0, 4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">1234</code></pre>
<p><code>substr(0, 4)</code> will keep the first 4 characters. Second argument exclude the argument position (upper bound excluded).</p>
<pre><code class="language-typescript">var string = "1234567890"
var substr = string.slice(-4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">890</code></pre>
<p><code>substr(-4)</code> will keep the last 4 characters.</p>
<h2>Substr()</h2>
<pre><code class="language-typescript">var string = "1234567890"
var substr=string.substr(4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">567890</code></pre>
<p><code>substr(4)</code> will remain everything after first 4 characters.</p>
<pre><code class="language-typescript">var string = "1234567890"
var substr = string.substr(0, 4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">1234</code></pre>
<p><code>substr(0, 4)</code> will keep the first 4 characters. Second argument denotes the number of characters to extract.</p>
<pre><code class="language-typescript">var string = "1234567890"
var substr = string.substr(-4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">890</code></pre>
<p><code>substr(-4)</code> will keep the last 4 characters.</p>
<h2>Substring()</h2>
<pre><code class="language-typescript">var string = "1234567890"
var substr=string.substring(4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">567890</code></pre>
<p><code>substr(4)</code> will remain everything after first 4 characters.</p>
<pre><code class="language-typescript">var string = "1234567890"
var substr = string.substring(0, 4);
console.log(substr);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">1234</code></pre>
<p><code>substr(0, 4)</code> will keep the first 4 characters. Second argument exclude the argument position (upper bound excluded).</p>
<p><strong>General note regarding indexes in all 3 methods:</strong> Strings are arrays of characters. Therefore they start character order from 0.</p>
<h2>Conclusion</h2>
<p>As you can see, using <code>substring</code> is nearly identical to <code>split()</code> or <code>substr()</code> method. However there are some small differences. For example <code>substring()</code> does not have option to add negative number and count string split backwards. Also if first argument is greater than the second, <code>substring</code> will swap the two arguments, and perform as usual.</p>
<p>Basically, if you know the the position (index) on which you will start and the length of the of characters to be extracted, use <code>subst(start_position, length)</code>. Otherwise if you know the position (index) you want to start and end, but <b>NOT</b> include the end position, use <code>slice(start_position, end_position_excluded)</code>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-show-first-n-characters-in-javascript-or-typescript-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
