DEV Community

Anees Abdul
Anees Abdul

Posted on

String in JS!

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


let fruits = "JackFruit";
console.log(fruits.length);
//Output:
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' ]
`
13. String indexOf():

  • It returns the index of the first occurrence of the substring in a string.

`javascript
let message = "JavaScript is not Java";
let index = message.indexOf("va");
console.log('index: ' + index); // index: 2
`

14. String lastIndexOf():

  • It returns the last index of occurence of a given substring in the string.
  • Both indexOf(), and lastIndexOf() return -1 if the text is not found

`javascript
let str = "I love JavaScript";
let result = str.lastIndexOf("Script")
console.log(result);
O/P:11
`

15. String search():

  • It searches for a string and returns the position of the match.

`javascript
let text = "Please locate where 'locate' occurs!";
text.search("locate");
O/P:
7
`

16. String match():

  • It returns the result of matching a string against a regular expression.
  • If you need to search the string entirely then use global search (/text/g)
  • If you want to perform case insensitive too then add (/text/gi)

`javascript
let text = "JavaScript is not a java"
console.log(text.match(/Ja/gi));
O/P:
['Ja', 'ja']
`

17. String matchAll():

  • It returns an iterator of results after matching a string against a regular expression.

`javascript
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/gi);
console.log([...iterator]);
`

`plaintext
0: ['cats']
1: ['Cats']
2: ['Cats']
`

18. String includes():

  • It returns true if a string contains a specified value.

`javascript
let message = "JavaScript is fun";
let result = message.includes("Java");
console.log(result);
O/P:
true
`

  1. String startsWith():
  2. The startsWith() method returns true if a string begins with a specified value.

`javascript
let text = "Hello world, welcome to the universe.";
let result=text.startsWith("Hello");
console.log(result);
O/P:
true
`

20: String endsWith():

  • The endsWith() method returns true if a string ends with a specified value. Otherwise return false

Top comments (0)