%%
date:: [[2023-05-07]]
parent::
%%
# [[String manipulation in js]]
Here's how to mess with strings in [[JavaScript]].
## Replace parts of a string with another string: `replace()`
```javascript
const session = "S83";
const lastGameNum = session.replace("S","#");
console.log(lastGameNum); // #83
```
## Removing part of a string
If you'd like to trim a string and only retain a part of it, there are *three* different options in JavaScript.
- `slice()`
- `substring()`
- `substr()`
### slice() vs substring() vs substr()
Here are the differences between the three: [^stackoverflow]
- *Syntax*: `substr()`'s second argument is the length, not the index of the character you want to cut to.
- `string.slice(start,end)`
- `string.substr(start,length)`
- `string.substring(start,end)`
- *Negative numbers*: `substring()` doesn't take negative numbers (the other two can). Negative numbers are useful for when you want to count from the end (right to left instead of left to right).
- *Behavior when first argument is greater than second*
- `slice()` doesn't do anything
- `substring()` executes normally, treating the second argument as the length
- `substr()` swaps the argument and then executes
> [!tldr] TL;DR: Use `slice()`
> Just use `slice()`. `substr()` is deprecated now, and `substring()` behaves a bit weirdly.
### Slicing strings with `slice()`
`slice()` takes two arguments: `slice(start,end)`. Both arguments represent the index of the character in the string that you want to *keep*. Using negative values as indices results in the index being calculated from the end of the string.
`slice()` des not change the actual string, so you'll need to assign it to a variable if you want to save the result.
```javascript
const noteName = "S01 2023-05-08 The Leaky Cucumber.md"
const episode = noteName.slice(0,-3)
console.log(episode); // S01 2023-05-08 The Leaky Cucumber
```
### Getting the index of a character or substring within a string
For `slice()`, `substr()`, and `substring()` above, you'll need to know the index you want to start at when selectively taking only a part of a string. The index refers to a character's position within a string. You *can* count this manually, but you can also find it out programmatically:
```javascript
const currentFileName = "S083 2023-05-08 The Leaky Cucumber.md"
const spaceIndex = currentFileName.indexOf(' '); // 4
const date = currentFileName.slice(spaceIndex+1,spaceIndex+11);
```
The last line above is equivalent to `currentFileName.slice(5,15`, except that you didn't need to know the exact numbers beforehand.
## Separate string by substring: `split()`
If you have a string that is punctuated by a character or substring and you'd like to separate the string along those characters, you can use `split()`:
```javascript
const currentFileName = "S083 2023-05-08 The Leaky Cucumber.md"
const fileNameArray = currentFileName.split(" ");
```
The `fileNameArray` array should contain five elements:
- `S083`
- `2023-05-08`
- `The`
- `Leaky`
- `Cucumber.md`
## Remove starting and leading spaces: `trim()`
To remove whitespace at the beginning and at the end of a string, use `trim()`:
```javascript
const currentFileName = " S083 2023-05-08 The Leaky Cucumber.md "
const currentFileNameTrimmed = currentFilename.trim() // S083 2023-05-08 The Leaky Cucumber.md
```
[^stackoverflow]: Waddah, van de Wal. (2022). *javascript - What is the difference between String.slice and String.substring? - Stack Overflow*. Retrieved from https://stackoverflow.com/a/31910656