0% found this document useful (0 votes)
14 views10 pages

String

Uploaded by

sodagep4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

String

Uploaded by

sodagep4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

MANIPULATE A STRING

• TEXT MUST BE TRANSFORMED INTO DATA ELEMENTS IF INFORMATION


GATHERED BY YOUR APPLICATION IS TO BE STORED IN A DATABASE,
WHICH IS LIKE AN ELECTRONIC FILING CABINET FOR PIECES OF DATA.
• FOR MANIPULATING STRINGS IN ADDITION TO CREATING DATA
ELEMENTS FROM TEXT.
JOINING STRINGS
• When you concatenate a string, you form a new string from two strings by placing a copy of the second
string behind a copy of the first string. The new string contains all the characters from both the first and
second strings.

• You use the concatenation operator (+) to concatenate two strings.

<script language="Javascript" type="text/javascript">

<!--

var newString = ‘Bharati' + ‘Vidyapeeth'

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.

<script language="Javascript" type="text/javascript">

<!--

var FirstName = ‘Bharati'

var Character = FirstName.charAt(0)

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.

• If the character is not in the string, the method returns –1.


<script language="Javascript" type="text/javascript">
<!--
var FirstName = ‘Bharati'
var IndexValue = FirstName.indexOf(‘r')
alert(IndexValue)
if(IndexValue != -1)
{
var Character = FirstName.charAt(IndexValue)
alert(Character)
}
-->
</script>
• To know the position of the character relative to the end of the string, you can
use the length value of the string object to calculate the position of the
character.

• var LengthOfString = string.length


• we can use the length value of the string to identify the index of the first of
the last four digits.
DIVIDING TEXT

• 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.

var NewStringArray = StringName.split('delimiter string')


<script language="Javascript" type="text/javascript">
<!--
var DataString = ‘Amol, Amit, Ashok, Atul, Ashish'
var NewArray = DataString.split(',')
document.write(DataString);
document.write('<br>')
for (i=0; i<4; i++)
{
document.write(NewArray[i])
document.write('<br>')
}
-->
</script>
COPYING A SUBSTRING

• 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>

You might also like