String L4
String L4
String–
Manipulate a string
• Joining a string
• Retrieving a character from given position
• Retrieving a position of a character
MANIPULATING A STRING
• Dividing a string into pieces or combining strings is called
manipulating a string
Joining Strings
• We can form a new string from two strings by placing a copy of
the second string behind the copy of the first string
• We can use concatenation operator (+) to concatenate two
strings
newString = firstString + secondString ;
MANIPULATING A STRING
Retrieving a character from given position
• We can retrieve a character from given position by using
charAt() method
• charAT() method requires one argument, which is the index of
character in the array
e.g
var ch = str_name.charAt(index);
MANIPULATING A STRING
Retrieving a position of a character
• You can determine index of a character by calling indexOf()
method of the string object
• It returns index of the character passed to if as an argument
• If the given character is not in the string, then it returns -1 value
e.g.
var index_val = str_name.indexof(‘character’);
MANIPULATING A STRING
Dividing text
• split() method of string object creates new array and then
copies portions of the string, called substring, into its array
elements
• You must tell the split() method what string/delimiter is used to
separate substrings in the string
e.g.
var new_arr = str_name.split(‘,’);
MANIPULATING A STRING
Copying a substring
• substring() is a method of string object that copies a substring
from a string based on a beginning and end position that is
passed as an argument to substring() method
• substr() method also returns a substring, but you must tell it the
starting position of the character that you want to include in the
substring and how many characters you want in the substring
from the starting position
e.g.
var new_sstr = str_name.substr(startposition, no_of_characters)
MANIPULATING A STRING
Converting a string to number and number to string
• The parseInt() method converts a number in a string to an
integer numeric value, which is a whole number
e.g.
var num = parseInt(StringName);
• Similarly parseFloat() method converts a string into floating
point number
• Numeric value can be converted to string by using toString()
method
e.g.
var num =100;
var str = num.toString();
MANIPULATING A STRING
Changing the case of the string
• String can be converted to uppercase using toUpperCase()
method
• String can be concerted to lowercase using toLowerCase()
method
• E.g.
strname.toUpperCase();
strname.toLowerCase();
FINDING UNICODE OF A CHARACTER
• Computer understands only numbers, not characters
• When you enter any letter, it is automatically converted into
number called Unicode number that the computer can
understand
• The String object method charCodeAt() takes an interger as an
argument that represents index of the character whose Unicode
you want to find
e.g. var un_no = StrName.charCodeAt(index);
• If you need to know the character, number or symbol that is
assigned to a Unicode number, use fromCharCode() method
e.g. var letter = String.fromCharCode(129);