Explain how to Manipulate the Strings in Java Script
https://www.computersprofessor.com/2016/06/explain-how-to-manipulate-strings-in.html
String refers to series of characters
(alphabets, special characters, numbers and combination of them) enclosed in
double quotations.
Most
of the data in java script is text strings, sum of the most useful text
manipulations involve ‘regular expression’ string. Calls allows you to store
text and also let’s you to do process on it.
There
are two ways to store text strings.
Syntax: var name = “ string”
(or)
var
name = new String (“ string name”);
String manipulations:
1. Joining strings together.
2. Spliting the string.
3.Searching for a letter (or) word in
the given string.
‘String
class’ contains number of methods:
Syntax: Objectname . Method( );
Methods:
1. charAt (index):
This function returns a character whose index
value is supplied as an argument to the function.
Eg: var
name = “SAI”;
document.write
(name. chartAt(2));
o/p: ‘I’
NOTE: If there is no character available
in the specified location then an empty string is returned.
2.concat (“String”[,”string”[,…….”stirng”]]);
It
combines two strings. Java script has two ways that it can join strings
together.
1) The
simplest way is to use the ‘+’ operator
Syntax: var name = prompt (“ enter you
name”,” “);
var age = prompt (“enter your
age”,” “);
var res = name +
age;
2) Using
‘concant’ key word.
Syntax:
Var masg = “ Thank
you”;
document. write (msg.concat (name));
3.indexOf (“search”L, offset]);
The
string is searched for the string (or) quoted character in the first parameter.
If
search is successful it returns the first occurrence of a specified string.
If the search is
unsuccessful the operation returns minus value.
By
default the index of function starts at index ‘O’.
Eg: var name = “nadeem”;
document. write (name. indexOf (“e”));
o/p:3
4. lastIndexOf
(“search”[,offset]):
This function also does exactly the same thing as
index of, but works its way backwards along the string.It
returns position of last occurrence of a specified position in a string.
Eg: var name = “nadeem”;
document. write (name.lastIndexOf
(“e”));
o/p:- 4
5.Length:
Value which holds the
no.of characters in a string. This is not a function so do not have to place
paranthesis when using it.
Eg. var name = “nadeem”l
var len = name.length;
o/p:- 6.
6. Split (separator [limit]):
It takes an argument of character and splits the source string into sub string
w.r.to the character supplied.
Eg:- var name = “nadeem”;
document.write (name.split(“e”));
o/p:- nad, m
split function breaks the string apart
whenever it encounters a character passed in as the first parameter.These
pieces of strings are stored in an array.
Split
has an optional second parameter which is an integer value indicating how many
of the pieces are to be stored in an array.
.7.substr(index [,length]):
The
function returns a substring which starts at the character indicated by index
parameter.
It
takes two arguments index, end/length respectively. here ‘index’ argument specifies
the initiating character, the length argument specifies the length of the
character to be displayed.
Eg:-
var name = “nadeem”;
document.
write (name. substr(1,2));
o/p:-
ad.
The
substring continuous either to the end of the string (or) the no.of characters
indicated by the length parameter. If the index is greater than the length of
the string then nothing is returned.
8.toLowerCase:
Converts all
characters in the strings to lower case. If the string contains few
non-characters then they remain unchanged.
9.toUpperCase:
Converts all
characters in the string to upper case.
Eg. var name = “nadeem”;
document.write (name.toUpperCase());
o/p:- NADEEM.
10.subString (index1[index 2]:-
This
function returns a set of characters which starts from index 1 and continuous
upto, but does not include the character at index 2.
It
takes two arguments index & end respectively here index argument specifies
the initiating character and the end specifies the last character to be
displayed.
Eg:- var name = “NADEEM”,
document.write (name. subString (1, 4));
o/p:- ade
The following rules are applied:
1) If index 1 is less than ‘o’ it will
be treated as ‘0’.
2) If index 2 is greater than the
length of the string it will be treated as the length of the string
3) If the two index values are equal
on empty string is returned.
4) If index 2 is missing all
characters upto the end of the string are taken.
5) If index 1 is greater than index 2 the run time
error occurs.
