You are here:   HomeJava ScriptJavascript StringJavaScript SubString
by Dinesh Thakur Category: Javascript String

JavaScript SubString Strings are constructed of characters. The JavaScript SubString method returns a set of characters within its calling String object. Its general syntax of JavaScript SubString method:

var NewSubstring = NewSubstring.substring (StartingPosition, EndPosition)

 

This JavaScript SubString method works much like the substr() method, but it allows you to send parameters for the Starting position and the ending position of the portion of the string you want to remove. It then returns the removed portion as a new string.




NewSubstring is any string. StartingPosition and EndPosition are both integers between 0 and NewSubstring.length – 1. StartingPosition is the index of the first character in the substring, whereas EndPosition is the index of the last character in the substring plus 1. The following script assigns the string "computer" to the variable segment:

 

<html>

<body>

<script type="text/javascript">

var strname = "ecomputernotes.com"

var segment = strname.substring(1, 9)

alert(segment);

</script>

</body>

</html>

 

As with all the methods of the String object so far, the character positions start at 0. However, you might be wondering why you specified the end character as 9. JavaScript SubString method is a little confusing because the end character is the end marker; it’s not included in the substring that is cut out. You can think of the parameters as specifying the length of the string being returned: the parameters 1 and 9 will return (9 - 1) characters starting at and including the character at position 1. Depicted graphically it looks like this:

 

Character Position     0  1  2  3  4  5  6  7  8  9 10 11 12 13

 Character                   e  c  o  m  p  u   t  e   r  n   o   t    e    s

 

Here's an another example of JavaScript SubString method

 

var myStr = new String("ecomputernotes");

alert(myStr.substring(9)); // alerts 'notes'

alert(myStr.substring(0)); // alerts 'ecomputernotes'

alert(myStr.substring(0, 9)); // alerts 'ecomputer'

 

The first time JavaScript SubString method was called, it was given '9' as a first argument, and no second argument. It went to index 9 of the String, which held the letter 'n', and selected through to the end of the String. The second time was the simplest one, substring() was given one argument, '0', which basically meant selecting from the first letter in the String through to the end of the String.It simply returned the entire String.The third time it was given two arguments: '0', and '9'. it's selecting from index 0 (the letter 'e'), through to, but not including, index 9 (the letter 'n'), so the resulting substring is "ecomputer".

 

Two scenarios come to mind with the arguments that substring() takes. First, What happens if the first argument is bigger than the second argument? This is handled very 'neatly': they're reversed. Every time substring() sees two arguments, it will always consider smaller number to be the first argument. So, in a sense, if you give substring() two arguments, it doesn't matter in what order you place them... substring() will 'make it work' for you (in a sense).

 

The second scenario is negative numbers. The substring() method handles negative numbers very simply: every number less than 0 equals 0. Again, very 'neat', isn't it? If substring() sees that any argument that was given to it is less than 0, then it will convert it to 0.

 

I should be clear that my writing 'neat' and 'neatly' isn't me being snarky, it's the purpose of the substring() method to automatically 'avoid problems' when you choose to use it, as opposed to the slice() method which will consider every option, and, as a result, will allow many unwanted results if you choose to use it in the 'wrong' place. But we'll get to slice() in a bit.

 

Here are some negative numbers in example of JavaScript SubString method:

 

var myStr = new String("ecomputernotes");

alert(myStr.substring(-9)); // alerts 'ecomputernotes'

alert(myStr.substring(-3, 9)); // alerts 'ecomputer'

alert(myStr.substring(9, -5)); // alerts 'ecomputer'

 

The first one is converted to one argument, '0', and so returns the entire String. The second is converted to substring(0, 9), and so returns 'ecomputer'. The third is first converted to substring(3, 0), and the arguments are reversed into: substring(0, 9), so you get: 'ecomputer'.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us