0% found this document useful (0 votes)
22 views

Css Practical No-5

Uploaded by

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

Css Practical No-5

Uploaded by

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

Practical No-5.Develop javascript to implement Strings.

JavaScript String
The JavaScript string is an object that represents a sequence of characters.
There are 2 ways to create string in JavaScript
1. By string literal
2. By string object (using new keyword)

1) By string literal
The string literal is created using double quotes. The syntax of creating string using string
literal is given
below:
var stringname="string value";
Example:
<!DOCTYPE html>
<html>
<body>
<script>
varstr="This is string literal";
document.write(str);
</script>
</body>
</html>

2) By string object (using new keyword)


The syntax of creating string object using new keyword is given below:
var stringname=new String("string literal");
Here, new keyword is used to create instance of string.
Example
<!DOCTYPE html>
<html>
<body>
<script>
varstringname=new String("hello javascript string");
document.write(stringname);
</script>
</body>
</html>

JavaScript String Methods


charAt() It provides the char value present at the specified index.
charCodeAt() It provides the Unicode value of a character present at the specified index.
concat() It provides a combination of two or more strings.
indexOf() It provides the position of a char value present in the given string.
astIndexOf() It provides the position of a char value present in the given string by
searching a character from the last position.
search() It searches a specified regular expression in a given string and returns its
position if a match occurs.
match() It searches a specified regular expression in a given string and returns that
regular expression if a match occurs.
replace() It replaces a given string with the specified replacement.
substr() It is used to fetch the part of the given string on the basis of the specified
starting position and length.
substring() It is used to fetch the part of the given string on the basis of the specified
index.
toLowerCase() It converts the given string into lowercase letter.
toUpperCase() It converts the given string into uppercase letter.
toString() It provides a string representing the particular object.
valueOf() It provides the primitive value of string object.

Example
<!DOCTYPE html>
<html>
<body>
<script>
// Get the character at the 3rd position (index 2) of the string "javascript"
var str = "javascript";
document.write(str.charAt(2)+<br>”); // Output: "v"

// Concatenate two strings


var s1 = "javascript ";
var s2 = "concat example";
var s3 = s1.concat(s2);
document.write(s3+<br>”); // Output: "javascript concat example"

// Find the index of the first occurrence of "from" in the string


var s1 = "javascript from javatpointindexof";
var n = s1.indexOf("from");
document.write(n); // Output: 11

// Find the last occurrence of "java" in the string


var s1 = "javascript from javatpointindexof";
var n = s1.lastIndexOf("java");
document.write(n); // Output: 22

// Convert the string to lowercase


var s1 = "JavaScript toLowerCase Example";
var s2 = s1.toLowerCase();
document.write(s2); // Output: "javascript tolowercase example"

// Convert the string to uppercase


var s1 = "JavaScript toUpperCase Example";
var s2 = s1.toUpperCase();
document.write(s2); // Output: "JAVASCRIPT TOUPPERCASE EXAMPLE"
</script>
</body>
</html>

Output

Conclusion:hence we have Develop javascript to implement Strings.

You might also like