What is String:
- A string is a sequence of characters, used to represent a text, and it should be enclosed in either "" or '' and can be used even in template literals, which allows embedding in backticks ``
Basic operations on Strings:
1. Find the length of a String:
- Returns the number of characters in a string
`javascript
let fruits = "JackFruit";
console.log(fruits.length);
`
`javascript
let fruits = "JackFruit";
console.log(fruits.length);
O/P:
9
`
2. String Replace:
- replace a substring/pattern in the string
`javascript
str.replace(pattern, replacement)
`
`javascript
let fruits = "JackFruit and JackFruit";
let result=fruits.replace("JackFruit", "Banana");
console.log(result);
O/P: Banana
`
- By default, replace() only replaces the first occurrence. To replace all occurrences, use a regular expression with the g flag.
`javascript
let fruits = "JackFruit and JackFruit";
let result=fruits.replace(/JackFruit/g, "Banana");
console.log(result);
O/P: Banana and Banana
`
3. String charAt():
- Returns character at a specified index in a string
`javascript
let fruits = "JackFruit";
console.log(fruits.charAt(3));
O/P:
K
`
4. String charCodeAt():
- Returns Unicode of the character at given index
`javascript
let fruits = "JackFruit";
console.log(fruits.charCodeAt(3));
O/P:
107
`
5. String concat():
- Concatenates the arguments to the calling string
`javascript
let fruit1 = "JackFruit";
let fruit2 = "Apple";
console.log(fruit1.concat(" and ",fruit2));
O/P:
JackFruit and Apple
`
6. String at():
- at() method returns the character at a specified index (position) in a string.
- at() supports negative index
`javascript
let str = "Hello";
console.log(str.at(-1));
O/P: o
`
7. String slice():
- Extracts and returns a section of the string
`javascript
const message = "JavaScript is fun";
// slice the substring from index 0 to 10
let result = message.slice(0, 10);
console.log(result);
// O/P: JavaScript
`
8. String substring():
- substring() is similar to slice().
`javascript
let message = "JavaScript is fun";
console.log(message.substring(3,10))
O/P: aScript
`
8. String toUppercase() and toLowervcase():
- Returns the uppercase/lowercase of a string
`javascript
let message = "JavaScript is fun";
console.log(message.toUpperCase());
//JAVASCRIPT IS FUN
let message = "JavaScript is fun";
console.log(message.toLowerCase());
//javascript is fun
`
9. String trim():
- Removes whitespace from both ends of a string
`javascript
let message = " JavaScript is fun";
console.log(message.trim());
O/P: JavaScript is fun
`
String trimStart():
- Remove whitespace only from the Starting point String trimEnd()
- Remove whitespace only at the End point
10. String padStart()
- Pads a string at the start to a given length
`javascript
let string1 = "CODE";
// padding "" to the start of given string
// until the length of final padded string reaches 10
let paddedString = string1.padStart(10, "");
console.log(paddedString);
// Output: ***CODE
`
* String padEnd()
- Pads a string at the end to a given length
`javascript
// string definition
let string1 = "CODE";
// padding "" to the end of the given string
// until the length of final padded string reaches 10
let paddedString = string1.padEnd(10, "");
console.log(paddedString);
// Output: CODE******
`
11. String repeat():
- Returns a string by repeating it given times
`javascript
let string1="CODE";
console.log(string1.repeat(2));
// Output: CODECODE
`
12. String split():
- Returns the string divided into a list of substrings
- Returns an Array of strings, split at each point where the separator occurs in the given string.
`javascript
const message = "JavaScript::is::fun";
// divides the message string at ::
let result = message.split("::");
console.log(result);
// Output: [ 'JavaScript', 'is', 'fun' ]
`
Top comments (0)