ava script tutorial - a new approach by Ken J Ward

Java Script Tutorial - Substrings

How to extract a string from a bigger string.


We can use charAt to extract a character from a string at a given position. We can extract a string from a bigger string by using substring. If we know the position of a string then we can extract it.


The above was written by JavaScript and tells us that the string in 'abcdefgh' between position 3 and position 6 is 'def'. Therefore we can use:

bigstring.substring(position1,position2);

to extract our string.

At first this seems a bit pointless and useless, but we can use this for a variety of purposes. However, first we need to check whether our string is in the big string. To do this we can use indexOf(). When we can do that, we can perform some useful tricks! Bear with me until we have looked at indexOf()!

The code for this page is:

<html>

<head>

<title>

Java Script Tutorial - Strings substrings - How to extract a string from a bigger

string.

</title>

</head>

<body>

<script language="javascript">

<!--//

document.write('This was written by JavaScript\n');

document.write('The result of \'abcdefgh\'.subString\(3,6\) is '+'abcdefgh'.substring(3,6));

//-->

</script>

</body>

</html>

Next: Finding out if a string is in a bigger one!

 

[If you need to learn more about HTML visit the HTML Tutorial]

I am always pleased to hear from you.
Send your comments to