AKM BHAI CSS (U-2) 5star Notes
AKM BHAI CSS (U-2) 5star Notes
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
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>;
● 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>;
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>
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>
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>
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>
Syntax:
parseInt(string);
Eg.
<script>
var str = "123";
var num = parseInt(str); // Converts "123" to 123
document.write(num); // Output: 123
</script>
Eg.
<script>
var str = "456";
var num = Number(str); // Converts "456" to 456
document.write(num); // Output: 456
</script>
Syntax:
number.toString();
<script>
var num = 456;
var str = String(num); // Converts 456 to "456"
document.write(str); // Output: "456"
</script>
Eg.
<script>
var text = "hello world";
var upperText = text.toUpperCase(); // Converts to "HELLO
WORLD"
document.write(upperText); // Output: HELLO WORLD
</script>
Syntax:
string.toLowerCase();
Eg.
<script>
let text = "HELLO WORLD";
let lowerText = text.toLowerCase(); // Converts to "hello
world"
document.write(lowerText); // Output: hello world
</script>
● 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().
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)