Beginning Javascript Course

String Methods

In days of old, there were three types of data - integers, variables, and strings. Integers were fixed numbers, variables could change, but were still only numbers. Strings on the other hand were the method of introducing letters into the equation. Strings ( often abreviated by the $ symbol ), could be used to represent letters, words, sentences, even entire paragraphs. Examples:

Hello (plain text)
Cool!!! (uses odd symbols - !)
Long strings of many words - even sentences and paragraphs
ou812 (combinations of letters and numbers)
!@#$%^&******()!

Strings were great for storing names, dates, etc - but while you could combine them to make longer strings, you couldn't perform mathematical functions on them. So once something was created as a variable or integer, you couldn't use letters, but if it was created as a string - even though it may be a number, you can't add, subtract and multiply without a lot of regurgitations.

In Javascript, Strings are not done away with - just incorporated ( some might say concotenated ) into the variable (var) method.
var myString ="This is microphone number 1";

This is a simple string actually. Javascript also has a more advanced method of handling strings, called String Objects. The neat thing is that JavaScript will automagically turn a String data type into a String OBJECT whenever it needs to as follows:
var myString = new String ("This is microphone number two");
This is a neat feature, because older languages had a hard time converting strings to variables to integers. Javascript does all this by nature, and with only one command word - var

Strings can be compared via logic operators ( == is equal to, != is not equal to ):

var stringOne = "This is Thing One";
var stringTwo = "and this is Thing Two";
if ( stringOne == string Two ) alert ("Strings are the same");
if ( stringOne != string Two ) alert ("Strings are NOT the same!");
alert (stringOne);


Strings can also be combined via concotenation using the + operator:

var stringOne = "This is Thing One";
var var stringTwo = "and this is Thing Two";
var stringThree = stringOne + "," + stringTwo + "!";
document.writeln(stringThree);


As far as STATEs goes, there is only one state for a string - how many characters long it is. If you need to examine the length (number of charachters) in a particular string, you can do so using the following method:
var stringOne = "This is Thing One!";
alert (stringOne.length);


This would yield a popup saying "18". Note that it counts spaces and punctuation marks as characters, because they are. There are several operational Methods in JavaScript that you can perform with a string, to include but not restricted to:
anchor stringObject.anchor ( anchorName )
Lets you create a string to use for the anchor text
big ( )stringObject.big ( )
myString="This is a test";
document.write(myString.big());
blink ( )stringObject.blink ( )
document.write(myString.blink));
bold ( )stringObject.bold ( )
document.write(myString.bold());
charAt ( )stringObject.charAt ( index )
Returns the Character at the position "index"
charCodeAt ( )stringObject.charCodeAt ( index )
Returns the LEN$() Value of the character at "index"
concat ( )stringObject.concat ( string1, string2, string3...stringN )
adds strings of text together
fixed ( )stringObject.fixed ( )
same as <tt></tt> in HTML
fontcolor ( )stringObject.fontcolor ( color )
fontsize ( )stringObject.fontsize ( size )
italics ( )stringObject.italics ( )
lastIndexOf ( )stringObject.lastIndexOf ( searchText [, index] )
Returns the CHR$() Value of the character at "index"
link ( )stringObject.link ( targetURL )
match ( )stringObject.match ( expression )
replace ( )stringObject.replace ( expression [, replacementText|function] )
search ( )stringObject.search ( expression )
slice ( )stringObject.slice ( start[, end ] )
small ( )stringObject.small ( )
split ( )stringObject.split ( delimiter [, count] )
strike ( )stringObject.strike ( )
sub ( )stringObject.sub ( )
substr ( )stringObject.substr ( start [, length] )
substring ( )stringObject.substring ( start [, end] )
sup ( )stringObject.sup ( )
toLowerCase ( )stringObject.toLowerCase ( )
toUpperCase ( )stringObject.toUpperCase ( )
valueOf ( )stringObject.valueOf ( )


Notice the any of the above style changes could also be written using HTML, for example - the italic style could be written as:

var italic_both = "<i>" + both + "</i>";

When using javascript to perform these functions, the document.writeln() METHOD can be used to write the results to a web page as follows:


document.writeln(stringOne.big());
document.writeln(stringTwo.fontcolor ( blue ) + "<br>");
document.writeln(stringThree.fontcolor ( #BBDDBB ) + "<&Amp;nbsp>");


Finally, you may have noticed that previously I said that a METHOD was a function attached to an OBJECT, however, when I used the prompt() METHOD, above, it was not attached to anything. Well, in fact it was, it was attached to the window OBJECT and it could have been written as: window.prompt(), it's just that window is the default OBJECT and ist's presence is assumed in the absence of anything else and so doesn't have to be written. Among the many window OBJECT METHODs is also alert().



On The Following Indicator...
( GREEN will indicate your current location)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

[ ElectronicsTheory.Com ]


THINK FREELY!  Keep Electronics Education Free for the masses by buying form the EE store!




This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1

VISITORS:
Add Me!