|
|
How long is a piece of string?
You are here: irt.org | Articles | JavaScript | Text, String, and Number | How long is a piece of string? Published on: Saturday 30th August 1997 By: Martin Webb
IntroductionThis article will describe some of the methods associated with the String object, and how to use them to find characters or strings within strings, to replace characters and portions of text, to change the case, and to alter the style and formatting of text. Properties and methodsThe main String property is length which returns the length of the string:
Which when run produces:
The String object has many methods associated with it. In the following, assume that text contains "abcxyzXYZxyz":
Find and ReplaceWe can use the indexOf method to find and replace items within a string. The following replace() function, takes three parameters, text the character or string to be replaced, by the character or string to replace it with and string which is to be modified. The first thing it does is test the length of string and text, and if one or the other is zero it retuns the current string. The variable i then holds the index of the first occurrence of text within the string using the indexOf() method. If i is null, i.e. !i meaning not found for JavaScript 1, and text is not located at the start of the string then it again returns the current string. This complicated check is required as !i is equivalent to a value of 0, and as 0 is a valid index, it is neccessary to double check before rejecting it. It then tests i for -1, which since JavaScript 1.1 also means not found, and again returns the current string. It then starts to create a newstr string based on the contents of the string upto the the i index, plus the replacement by string. A check is then made to see if the length of the text to be replaced is less than the length of the current string, if it is then it reiterates the replace() function, using the remainder of the current string. The last thing it does is to return the newstr string.
Which when run produces something like the following:
I've mentioned the phrase 'current string' quite a few times. The reason being, that because the replace() function is interated until there are no more occurences of the text string within the string, then the string passed is constantly changing through each iteration. For example when replacing the character 'o' within 'Six of one and half a dozen of the other' the different current strings through the iterations are:
One potential use of the replace() function is to highlight a particular phrase, for example:
Another is to remove items from a string:
UPPER and lower CaSeThere are two String object methods toLowerCase() and toUpperCase() which change the case of the string. The following example reuses the replace() function to convert the first character of the text string within the string to upper case using the toUpperCase() method. The upperFirst() function uses the charAt(0) method to locate the first character of text string which is changed to upper case and then appends it with the remainder of the text string using the substring() method.
Which when run produces:
This example can be extended by placing the output of the upperFirst() function as the input to yet another upperFirst() function, for example:
Which when run produces:
Rather than pass the output of one function into the input of another it is possible to pass the parameters all in one go to an intermediate function. In the following example the multipleUpperIntial() function accepts the first parameter string and then processes all the remaining arguments using the length property of the arguments, each one in term is passed to the upperInitial() function.
Which when run produces the following:
Style and PresentationThere are 11 String object methods that effect the style of text. For example to make text bold:
Which when run produces the following:
The following diplays the complete range available:
Which produces:
Even silly special effects can be produced. For example, to produce the following using HTML would require 37 sets of <FONT SIZE=i>x</FONT>:
Whereas it uses a simple scale() function to accept a string which is then altered a character at a time using the fontsize() method.
Once the scale() function has been defined it can be used again and again:
The following example demonstrates what the methods are doing. If you select any of the checkboxes you will see that the text is wrapped with the appropriate HTML commands required to change the style of the text. For example, if you select bold, the the testForm will show the value of the string as <B>text</B>. Which is why, if you were to write the value of the string to the document using document.write() the string would render as bold. The String SectionYou can easily create a string library (stringLib.js) containing all your string functions that you create, for example:
Notice that there is no HTML whatsoever in the file - THIS IS IMPORTANT! Then all your string functions are available to any and all your documents with the inclusion of one line:
Alright then - two lines. For more information on JavaScript *.js source files look at Source Files. Feedback on 'How long is a piece of string?'
View the profile on Martin Webb and the list of other Articles by Martin Webb. |
-- div -->
|