Join Regular Classroom : Visit ClassroomTech

Data Structure – codewindow.in

Related Topics

Data Structure

my_string = "This is a string that we want to split"
my_list = my_string.split(" ")
print(my_list)

Output:

['This', 'is', 'a', 'string', 'that', 'we', 'want', 'to', 'split']

In this example, we split the string my_string using the space character as the delimiter, resulting in a list of substrings. The resulting list is stored in the variable my_list.

// Declaring and initializing a string using double quotes
let str1 = "Hello, world!";

// Declaring and initializing a string using single quotes
let str2 = 'This is a string.';

// Declaring and initializing an empty string
let str3 = "";

In addition, you can also declare and initialize a string using the String() constructor, like this:

let str4 = new String("This is another string.");

However, using the String() constructor is less common than using the string literal syntax.

let str1 = "Hello";
let str2 = "World";
let result = str1 + " " + str2;
console.log(result); // Output: "Hello World"

 And here’s an example of using the concat() method:

let str1 = "Hello";
let str2 = "World";
let result = str1.concat(" ", str2);
console.log(result); // Output: "Hello World"

Both methods produce the same output, which is a concatenated string.

const str = "Hello, world!";
const len = str.length;

console.log(len); // Output: 13

In this example, the length property is used to find the length of the string "Hello, world!". The resulting value is stored in the variable len, which is then outputted to the console.

let string1 = "Hello";
let string2 = "World";
let string3 = "Hello";

console.log(string1 === string2); // false
console.log(string1 === string3); // true
console.log(string1 == string3); // true

In this example, string1 is compared to string2 and the result is false since the two strings have different values. string1 is then compared to string3 using both the === and == operators, and the result is true in both cases since string1 and string3 have the same value.

let str = "hello world";
let reversedStr = str.split("").reverse().join("");
console.log(reversedStr); // "dlrow olleh"

In this example, the split("") method splits the string into an array of individual characters, the reverse() method reverses the order of the array elements, and the join("") method joins the array elements back into a string.

Another approach is to use a for loop to iterate through the characters of the string and construct a new string in reverse order:

let str = "hello world";
let reversedStr = "";
for (let i = str.length - 1; i >= 0; i--) {
  reversedStr += str[i];
}
console.log(reversedStr); // "dlrow olleh"

In this example, the for loop iterates through the characters of the string in reverse order and appends each character to the reversedStr variable.

let str = "Hello, World!";
if (str.includes("o")) {
  console.log("The string contains 'o'");
}

Output:

The string contains 'o'

To check if a string contains a specific substring, you can use the includes() method or the indexOf() method as follows:

let str = "Hello, World!";
if (str.includes("World")) {
  console.log("The string contains 'World'");
}

if (str.indexOf("World") !== -1) {
  console.log("The string contains 'World'");
}

Output:

The string contains 'World'
The string contains 'World'

Note that the includes() method returns a boolean value indicating whether or not the string contains the specified character or substring, while the indexOf() method returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.

let str = "Hello World";
let index = str.indexOf("W"); // returns 6

In this example, the indexOf() method is used to find the index of the letter “W” in the string “Hello World”. The returned index is 6, which is the index of the first occurrence of the letter “W” in the string.

You can also specify a starting index for the search by passing a second argument to the indexOf() method:

let str = "Hello World";
let index = str.indexOf("o", 5); // returns 7

In this example, the indexOf() method is used to find the index of the letter “o” in the string “Hello World”, starting from the index 5. The returned index is 7, which is the index of the second occurrence of the letter “o” in the string.

string.substring(startIndex, endIndex)
string.slice(startIndex, endIndex)

Here, startIndex is the index of the first character to include in the substring (inclusive), and endIndex is the index of the last character to include in the substring (exclusive). If endIndex is omitted, the method will return the substring from startIndex to the end of the string.

Here’s an example:

let str = "Hello, world!";
let substring1 = str.substring(0, 5); // "Hello"
let substring2 = str.slice(7, 12); // "world"
let substring3 = str.slice(7); // "world!"

In the example above, substring1 extracts the first five characters of str, substring2 extracts the five characters starting at index 7, and substring3 extracts all characters from index 7 to the end of the string.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories