Top Ad unit 728 × 90

what is substring in javascript - difference between substring and subsequence

How Substring is defined in Java Script Language: It is a subset of another string. It's a contiguous sequence of characters within a string or a sentence. Lets take an example, 'Welcome to ASENwebmedia' is a substring of the string 'Welcome to ASENwebmedia Online Blog and Tutorials'. But if you think for 'Online Tutorials' then it is not a substring as it is a subsequence part or you can say the generalization part of the string or a sentence. 

Substring function in JavaScript:
This is an inbuilt function in JavaScript which returns a part of the string given by user from start index to end index. Here starting indexing initialize with zero and it is used to return a particular portion of the string, starting at the specified index and extending for a given number of characters afterward. Substring function takes two parameters and both are integer. I am taking previous example.

In Code: var a_string = "Welcome to ASENwebmedia Online Blog and Tutorials";
          a_string = a_string.substring(0, 23);  //Output is 'Welcome to ASENwebmedia'
  a_string = a_string.substring(11, 23);  alert(a_string);  //Output is 'ASENwebmedia'

Using alert() you can see the output in the browser. here in above two examples first parameter has been taken as 0 and 11 which are defines starting index or starting position and second parameter has been taken as 23 for both cases and it defines the length of the substring. 

Now if want to find out a length of a string then you have to use 'length' property and you should remember length is not a function. So, lets see examples.

In Code: var a_string = "Welcome to ASENwebmedia";
         var b_lenth = a_string.length; //output is 23, so, length function return a number of length of a given string or sentence.



what is substring in javascript - difference between substring and subsequence Reviewed by Ashok Sen on 21:52:00 Rating: 5

2 comments:

Nancy said...

How substr function works in php?

In my project I have used four variables.

In first variable $composite_code = '4701053'; 7 digits is assigned and now, first 3 digits will be stored in $d_major_code and second 2 digits will be stored in $d_minor_code and third 2 digits will be stored in $d_ac_code

So, how can I write the code for this? Could anyone please help? Thanks.

Ashok Sen said...

Thanks for your question. The syntax of substr function in php is
substr(string, start position)
or
substr(string, start position, length of no of characters from start position)
Lets check your example.

$d_major_code = substr($composite_code, 0, 3); echo $d_major_code;
$d_minor_code = substr($composite_code, 3, 2); echo $d_minor_code;
$d_ac_code = substr($composite_code, 5, 2); echo $d_ac_code;

Output will be
470
10
53

That's all. Further any please reply.

Website Design Company at Kolkata All Rights Reserved © 2014 - 2019
Developed by Asenwebmedia

Contact Form

Name

Email *

Message *

Powered by Blogger.