String
String
<!--
alert(newString)
-->
</script>
RETRIEVING A CHARACTER FROM GIVEN POSITION
• Each character in a string is an array element that can be identified by its index.
• copy a character from a string to another string by using the charAt() method of the string object. The
charAt() method requires one argument, which is the index of the character that you want to copy.
<!--
alert(Character)
-->
</script>
• Determine the index of a character by calling the indexOf() method of the string object. The
indexOf() method returns the index of the character passed to it as an argument.
• split():
The split() method creates a new array and then copies portions of the
string, called a substring, into its array elements. You must tell the split() method
what string (delimiter) is used to separate substrings
in the string.
• substring():
The substring()is a method of a string object that copies a substring from a string based on a beginning
and an end position that is passed as an argument to the substring() method. The starting position
specifies the first character that is returned by the substring() method—that is, the first character in the
substring. The end position specifies the character that follows the last character that is returned by the
substring() method.
• var NewSubstring = StringName.substring (StartingPosition, EndPosition)
<script language="Javascript" type="text/javascript">
<!--
var EmailAddress = ‘[email protected] '
var NewSubstring = EmailAddress.substring(7,14)
var Client = 'www.' + NewSubstring
alert(‘The client web site is: ', Client )
-->
</script>
• substr():
The substr() method returns a substring. You must tell it the starting position of the first character that
you want to include in the substring and how many characters you want copied into the substring from the
starting position. Both positions are passed as arguments to the substr() method.
<script language="Javascript" type="text/javascript">
<!--
var EmailAddress = prompt('Enter your clients email address.', ' ')
var StartPosition = EmailAddress.indexOf('@') + 1
var NumCharactersToCopy = EmailAddress.length - StartPosition
var NewSubstring = EmailAddress.substr(StartPosition, NumCharactersToCopy)
var GuessWebSite = 'www.' + NewSubstring
alert(‘The client web site.'+GuessWebSite )
-->
</script>