0% found this document useful (0 votes)
16 views40 pages

AKM BHAI CSS (U-2) 5star Notes

notes

Uploaded by

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

AKM BHAI CSS (U-2) 5star Notes

notes

Uploaded by

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

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2.4 STRING

● Strings are used for storing and manipulating text.


● String is zero or more characters written in quotes.
● A string in javascript is a sequence of characters used to represent text.
● Strings are immutable, means once string is created it cannot be modified.
● String can be created using single quotes(‘’) , double quotes(“”) etc.

Syntax:
var str2 = "This is also a string";
Eg.<>
<script>
var greeting ="Hello";
var name="aryan";
//concatenation
var message = greeting +","+ name + "!";
document.write(message);
// o/p: Hello,aryan!;
</script>;

2.4.1 Manipulating a string.

● String manipulation means operation on string.


● String manipulation refers to performing various operations on string, such
as changing the case, extracting and replacing parts of the string.
The operations are:
1. Concatenation
2. Extraction of sub string
3. Finding length
4. Finding position of character
5. Changing case of string character etc.

2.4.2 Joining a string.

● Joining strings refers to the process of combining two or more strings into
one.
● This is commonly done using the concat() method or the + operator.
● The concat() method is used to join to or more strings
● Strings can also be joined using the plus operators.
● Strings can be joined using template literals (backticks ` `), which allows
embedding expressions.

Syntax:
string1.concat(string2, string3, ...);

E.g
<script>
var firstname = "john";
var lastname = "thomas";
// Joining strings using the concat() method
var fullname = firstname.concat("",lastname);
document.write("fullname="+fullname);
// Output: Full Name: John Doe
</script>;

2.4.3 Retrieving a character from given position.

● In JavaScript, you can retrieve a character from a specific position in a


string using the charAt() method or bracket notation
● The charAt() method is used to return the character at a specified index.
● You can also use bracket notation to retrieve a character from a specific
position.
● In JavaScript, string indices start at 0, meaning the first character is at
index 0, the second character at index 1, and so on.

Syntax:
string.charAt(index);
<script>
let str = "JavaScript";
// Retrieves the character at index 4
let char = str.charAt(4);
document.write(char);
// Output: "S"
</script>

2.4.4Retrieving the Position of a Character in a String

● In JavaScript, you can find the position (index) of a specific character or


substring in a string using the indexOf() method.
● The indexOf() method returns the index of the first occurrence of a
specified character or substring within the string. If the character is not
found, it returns -1.
● The indexOf() method is case-sensitive, meaning "S" is different from
"s".
● You can also use indexOf() to find the position of a substring.
● The search() method is used to find the position of a substring or
regular expression in a string. It returns the index of the first matc
● he lastIndexOf() method returns the last occurrence of a specified
character or substring in a string. If the character is not found, it returns -1.
● This method searches the string from right to left (backward), but the index
is still counted from the start (left to right).

Syntax
string.indexOf(character);
string.search(substring);
string.lastIndexOf(substring);
<script>
let str = "JavaScript";
// Finds the position of "S"
let position = str.indexOf("S");
document.write(position); // Output: 4
</script>
<script>
let text = "Hello World, Welcome to the World!";
let position = text.lastIndexOf("World");
document.write(position); // Output: 24
</script>
<script>
var text = "Hello World";
var position = text.search("World");
document.write(position); // Output: 6
</script>

2.4.4 Dividing Text.


● you can divide a string into multiple parts using the split() method.
● This method splits a string into an array of substrings based on a specified
delimiter (like a space, comma, or any character).
● The split() method divides a string at each occurrence of the specified
separator and returns an array containing the substrings.
● You can specify any character or substring as the separator to divide the
string.
● You can also specify a limit to control the number of splits.

Syntax:
string.split(separator);
<script>
var text = "Hello World Welcome";
var words = text.split(" "); // Divides the text by spaces
document.write(words); // Output: ["Hello", "World",
"Welcome"]
</script>

Copying a Substring
● In JavaScript, you can copy a part of a string (substring) using methods
like substring(), substr(), or slice(). These methods extract parts
of a string and return a new string.
● The substring() method extracts characters from a string between two
specified indices and returns the new substring.
● The extraction starts at startIndex and goes up to (but does not include)
endIndex. If endIndex is omitted, it extracts till the end of the string.

Syntax:
string.substring(startIndex, endIndex);

<script>
var text = "Hello, World!";
var result = text.substring(0, 5); // Extracts from
index 0 to 4
document.write(result); // Output: "Hello"
</script>

● The substr() method extracts a substring starting from a specific index


and for a specified number of characters.

Syntax:
string.substr(startIndex, length);
<script>
var text = "JavaScript";
var result = text.substr(4, 6); // Extracts 6 characters
starting from index 4
document.write(result); // Output: "Script"
</script>

Converting strings to numbers and numbers to string.

● In JavaScript, you can convert a string to a number using various methods


like parseInt(), parseFloat(), and the Number() function.
● The parseInt() method converts a string into an integer (whole
number). It ignores decimal places.

Syntax:
parseInt(string);

Eg.
<script>
var str = "123";
var num = parseInt(str); // Converts "123" to 123
document.write(num); // Output: 123
</script>

● The parseFloat() method converts a string to a floating-point (decimal)


number.
Syntax:
parseFloat(string);
<script>
var str = "123.45";
var num = parseFloat(str); // Converts "123.45" to 123.45
document.write(num); // Output: 123.45
</script>
● The Number() function converts a string to a number (integer or float
depending on the value).
Syntax:
Number(string);

Eg.
<script>
var str = "456";
var num = Number(str); // Converts "456" to 456
document.write(num); // Output: 456
</script>

Converting Numbers to Strings:


● You can convert a number to a string using the toString() method or
the String() function.
● The toString() method converts a number into a string.

Syntax:
number.toString();

The String() function converts a number to a string.


Syntax:
String(number);

<script>
var num = 456;
var str = String(num); // Converts 456 to "456"
document.write(str); // Output: "456"
</script>

Changing the Case of a String:


you can change the case of a string to uppercase or lowercase using two
methods: toUpperCase() and toLowerCase().
The toUpperCase() method converts all characters of a string to uppercase.
Syntax:
string.toUpperCase();

Eg.
<script>
var text = "hello world";
var upperText = text.toUpperCase(); // Converts to "HELLO
WORLD"
document.write(upperText); // Output: HELLO WORLD
</script>

The toLowerCase() method converts all characters of a string to lowercase.

Syntax:
string.toLowerCase();

Eg.
<script>
let text = "HELLO WORLD";
let lowerText = text.toLowerCase(); // Converts to "hello
world"
document.write(lowerText); // Output: hello world
</script>

Finding a Unicode of a character - charCodeAt(), fromCharCode().

● you can find the Unicode value of a character using the charCodeAt()
method and convert a Unicode value back to a character using the
fromCharCode() method.
● The charCodeAt() method returns the Unicode of the character at a
specified position in a string.
● This method is used to return a unicode of specified character.
● Here, index is the position of the character in the string (starting from 0).
Syntax:

string.charCodeAt(index);

Eg.

<script>
var text = "Hello";
var unicode = text.charCodeAt(0); // Finds the Unicode of
the character at index 0 ('H')
document.write(unicode); // Output: 72 (Unicode value of
'H')
</script>

fromCharCode().

● This method is used to return a character for specified code.


● This method is used to return a unicode of specified character.

Syntax:
String.fromCharCode(unicode);

<script>
var character = String.fromCharCode(72); // Converts
Unicode 72 to the character 'H'
document.write(character); // Output: H
</script>
Model ANSER PAPER MASTERY:

1. Write the use of chatAt() and indexof() with syntax and example.
(W-19 4m)
2. Write a JavaScript that will replace following specified value with
another value in string
String = “I will fail”
Replace “fail” by “pass” (W-19 4m)
3. Write a javascript function that accepts a string as a parameter and
find the length of the string.(S-22 4m)
4. Write a javascript to checks whether a passed string is palindrome or
not. (S-22 6M)
5. Develop javascript to convert the given character to unicode and
vice-versa. (S-22 6m)
6. Write and explain a string functions for converting string to number
and number to string. (W-22 4m)
7. Write a JavaScript function to check the first character of a string is
uppercase or not. (W-22 4m)
8. Write the use of charCoadeAt() and fromCharCode() with syntax and
example. (S-23 4m)
9. Write javascript code to perform following operations on string
. Use split() method. (S-23 4m)
Input String:”Sudha Narayan Murthy”
Display output as
First Name: “Sudha”
Middle Name: “Narayan”
Last Name: “Murthy:
13. State the use of following methods: i) charCodeAt( ) ii)
fromCharCode () (S-24 4m)
14. Write a JavaScript function that checks whether a passed string is
palindrome or not. (S-24 4m)

You might also like