# JS Basic Datatypes - String

A series of characters inside quotation marks, apostrophes or backticks.

```js
let string = "Hello world!";
let string2 = 'Hello world!';
let string3 = `Hello world!`;
```

## Escape characters inside a string

If you want to contain a character inside a string which indicates the start and end of string you have to escape that character to consider it as a regular character.

```js
let string = ""Hello world!" - John Doe";
// this throws error because it's interpreted as an empty string ("") then something that doesn't fit any datatype and another string: " - John Doe"
```

Let's fix it by adding backslashes before the inner quotation marks.

```js
let string = "\"Hello world!\" - John Doe"; // '"Hello world!" - John Doe'
```

It works the same way with apostrophes and backticks.

## Add line break (\n)

```js
let string = "\"Hello world!\"\nJohn Doe";
```

## Concatenation

With + operator:

```js
let name = "Fossery";
let string = "Hello " + name + "!"; // "Hello Fossery!"
```

With expression inside string (only works with strings indicated with backtick!):

```js
let name = "Fossery";
let string = `Hello ${name}!`; // "Hello Fossery!"
```

With concat() method:

```js
let name = "Fossery";
let string = "Hello ".concat(name, "!"); // "Hello Fossery!"
```

## Get length of string (counting from 1)

```js
let string = "Hello world!";
let lengthOfString = string.length; // 12
```

## Get a character at a specific index (counting from 0)

Treat it like an array:

```js
let string = "Hello world!";
let firstChar = string[0]; // "H"
let hundredthChar = string[99]; // undefined
```

Use charAt() method:

```js
let string = "Hello world!";
let firstChar = string.charAt(0); // "H"
let hundredthChar = string.charAt(99); // ""
```

Use at() method (also supports negative indexing):

```js
let string = "Hello world!";
let firstChar = string.at(0); // "H"
let lastChar = string.at(-1); // "!"
let hundredthChar = string.at(99); // ""
```

## Get all characters from start index to end index

Use subtring() method:

```js
let string = "Hello world!";
let hello = string.substring(0, 5); // "Hello" (0 is inclusive, 5 is exclusive)
let world = string.substring(6); // "world!" (if end index is not provided it matches until the last character of the string)
```

Use slice() method (also supports negative indexing):

```js
let string = "Hello world!";
let hello = string.slice(0, 5); // "Hello" (0 is inclusive, 5 is exclusive)
let world = string.slice(-6, -2); // "world"
```

## Check if string includes another string

```js
let string = "Hello world!";
let string2 = "Hello";
let string3 = "hello";
let includesString2 = string.includes(string2); // true
let includesString3 = string.includes(string3); // false
```

## Check if string starts with another string

```js
let string = "Hello world!";
let string2 = "Hello";
let string3 = "world!";
let startsWithString2 = string.startsWith(string2); // true
let startsWithString3 = string.startsWith(string3); // false
```

## Check if string ends with another string

```js
let string = "Hello world!";
let string2 = "Hello";
let string3 = "world!";
let endsWithString2 = string.endsWith(string2); // false
let endsWithString3 = string.endsWith(string3); // true
```

## Get starting index of first occurrence of a search term

```js
let string = "Hello world! Nice to meet you, world!";
let searchTerm = "world";
let whereIsFirstWorld = string.indexOf(searchTerm); // 6
```

## Get starting index of last occurrence of a search term

```js
let string = "Hello world! Nice to meet you, world!";
let searchTerm = "world";
let whereIsLastWorld = string.lastIndexOf(searchTerm); // 31
```

## Create modified version of a string by replacing a piece of text

Replace only the first occurrence using replace() method:

```js
let string = "Hello world! Nice to meet you, world!";
let newString = string.replace("world", "Fossery"); // "Hello Fossery! Nice to meet you, world!"
```

Replace all occurrences using replaceAll() method:

```js
let string = "Hello world! Nice to meet you, world!";
let newString = string.replaceAll("world", "Fossery"); // "Hello Fossery! Nice to meet you, Fossery!"
```

## Create a new array from a string splitted by a pattern (string or regex)

```js
let string = "Hello world! Nice to meet you, world!";
let words = string.split(" "); // ["Hello", "world!", "Nice", "to", "meet", "you,", "world!"]
```

For array operations, check out the [Arrays cheatsheet](https://fosseryweb-min.codeberg.page/cheatsheets/js/array.html).

## Create uppercase version of a string

```js
let string = "Hello world! Nice to meet you, world!";
let uppercaseString = string.toUpperCase(); // "HELLO WORLD! NICE TO MEET YOU, WORLD!"
```

## Create lowercase version of a string

```js
let string = "Hello world! Nice to meet you, world!";
let lowercaseString = string.toLowerCase(); // "hello world! nice to meet you, world!"
```

## Create new string from original without whitespaces at the start and end

```js
let string = "        \n              Hello world! Nice to meet you, world!     \n      ";
let trimmedString = string.trim(); // "Hello world! Nice to meet you, world!"
```
