# String methods cheatsheet

[Download image version](https://github.com/cheatjs/cheat/blob/master/JavaScript/String-methods/js-string-methods.png)

## Changing the case

* **toLowerCase / toUpperCase**()\\

```js
const str = "Hello";
const low = str.toLowerCase(); // hello
const up = str.toUpperCase(); // HELLO
```

***

## Combining strings

* **concat**(...items)\
  \&#xNAN;*Combines two or more strings and returns one string*

```js
const str = "Hello";
const str2 = "World!";
const newStr = str.concat(" ", str2);
// newStr = "Hello World!"
```

***

## Splitting a string

* **split**(separator, limit)\
  \&#xNAN;*Splits a string into an array by the specified separator, which can be a substring or a regular expression*

```js
const str = "Just some text";
const arr = str.split(" ");
arr = ["Just", "some", "text"];
```

***

## Repeating a string

* **repeat**(count)\
  \&#xNAN;*Repeats the string the specified number of times*

```js
const str = "hey";
const newStr = str.repeat(3);
// newStr = "heyheyhey"
```

***

## Substring search

* **charAt**(index)\
  \&#xNAN;*Returns a character at the specified index*

```js
const str = "hello";
const char = str.charAt(1);
// char = "e"
```

* **includes**(substr, startPositon)\
  \&#xNAN;*Checks if the string contains the specified substring and returns true/false*

```js
const str = "example@gmail.com";
str.includes("gmail"); // true
```

* **indexOf / lastIndexOf**(substr, startPositon)\
  \&#xNAN;*Returns the index of the first/last substring found, otherwise returns -1*

```js
const str = "example@gmail.com";
str.indexOf("@"); // 6
str.lastIndexOf("m"); // 15
```

* **endsWith / startsWith**(substr, searchLength)\
  \&#xNAN;*Checks if the string ends/starts with the specified substring and returns true/false*

```js
const link = "www.google.com";
link.startsWith("www"); // true
link.endsWith(".com"); // true
```

* **search**(substr)\
  \&#xNAN;*Checks if there is a specified substring or regular expression in the string and returns the index of the beginning of the match*

```js
const str = "first name, second name";
str.search("name"); // 6
```

***

## Extracting a substring

* **slice**(start, end)\
  \&#xNAN;*Extracts part of the string starting at the `start` position and ending at the `end`-1 position*

```js
const str = "javascript";
const substr = str.slice(4); // script
const substr = str.slice(0, 4); // java
```

* **substring**(start, end)\
  \&#xNAN;*Extracts substring from a string between `start` and `end`. Unlike `slice()`, you can set `start` more than `end`.*

```js
const str = "javascript";
const substr = str.substring(4); // script
const substr2 = str.substring(4, 0); // script
```

* **substr**(start, length)\
  \&#xNAN;*Extracts part of a string of a specified `length`. The first parameter can be negative, then the position is determined from the end of the string.*

```js
const str = "typescript";
const sub = str.substr(4, 6); // script
const sub2 = str.substr(-6); // script
```

***

## Replacing a substring

* **replace**(substr, newSubstr)\
  \&#xNAN;*Searches for the specified substring (or regular expression) in the string and replaces it with another one*

```js
const str = "typescript";
const str2 = str.replace("type", "java");
// str2 = javascript
```

* **replaceAll**(substr, newSubstr)\
  \&#xNAN;*Replaces all found matches with another string or passed function*

```js
const str = "one two three";
const str2 = str.replaceAll(" ", "-");
// str2 = one-two-three
```

***

## Adding characters

* **padStart / padEnd**(strLength, str)\
  \&#xNAN;*Adds padding at the beginning/end of a string until the string reaches the length specified by the first parameter*

```js
const msg = "hello";
msg.padStart(10, "_"); // "_____hello"
msg.padStart(7); // "  hello"
msg.padEnd(10, "*"); // "hello*****"
```

***

## Removing spaces

* **trim / trimStart / trimEnd**()\
  \&#xNAN;*Deletr spaces at both ends of the string, either only at the beginning or only at the end*

```js
const str = "  hello   ";
str.trim(); // "hello"
str.trimStart(); // "hello   "
str.trimEnd(); // "  hello"
```

***

## Working with ASCII code

* **charCodeAt**(index)\
  \&#xNAN;*Returns the ASCII code of the character at the specified `index`*

```js
const str = "AaBbCc";
str.charCodeAt(); // 65 (because it's "A")
str.charCodeAt(3); // 98 (because it's "b")
```

* **fromCharCode**(...code)\
  \&#xNAN;*Converts ASCII code to readable characters*

```js
const str = String.fromCharCode(74, 83);
// str = "JS"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yurace.gitbook.io/cheatjs/javascript/js-string-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
