String Manipulation Methods to Memorize
Tags: javascript, strings, methods, techinterviews
Published: 2022-03-05 04:38:32

A common tech interview question I’ve received a lot is on [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) manipulation. This involves a request to return a desired value out of a given string.

In this blog, I list down the most common string manipulation methods to memorize so you can easily answer such questions when asked in tech interviews. 

![strings](https://media.giphy.com/media/J1XPgsiD4QQ4WsVyKG/giphy.gif)


###Stringing Along
I was recently asked this coding challenge in an interview which was fairly easy if I wasn't so nervous and had only studied data structures and algorithms the week before. 

```
// Write a function called "abbreviate" that outputs a string.  
// Given a string, keep the first and last letter and replace the letters in between by the length. 
// e.g. internationalization => i18n, localization => l10n
```
It took me longer than usual to remember what methods to use to achieve the output desired. Of course, using the handy `console.log`, I was able to test out some possible methods, but I still had to search for specific ones online before getting the solution. Below was what I came up with: 

```
const abbreviate = (input) => {
    return input.length < 3 ? input : [input[0], input.length - 2, input[input.length-1]].join(''); 
};

const result = abbreviate("internationalization");

console.log(result);
```
This in turn made me reevaluate ways to etch these common string manipulation methods in my head. As a result, I'm compiling the cheat list below to assist with that. 


###Common String Manipulation Methods 
![violin strings break](https://media.giphy.com/media/5zkDXMbwJ7OiUT9vyn/giphy.gif)


#### str.length
- returns the length of the string 

```
let str = "zen"; 
console.log(str.length); // outputs 3 
```

#### charAt(index)
- treats the string as an array of characters 
- retrieves the character at the index provided 
- used to check string for consistency 
- the last index is `string.length - 1` 

```
let str = 'Strings';
console.log(str.chatAt(3)); // outputs i 
console.log(str.charAt(6)); // outputs s
```

#### concat(string)
- concatenate two string together into one 
- used to append to a string or combine them 

```
const str1 = 'purple';
const str2 = 'balloon'; 
console.log(str1.concat(str2)); // outputs 'purple balloon' 

// or by inserting string variables into another string to achieve cleaner code 

const str1 = 'purple';
const str2 = 'balloon';
const sampleStr = `${purple} ${balloon}`; 
console.log(sampleStr); // outputs purple balloon 

```

#### includes(string)
- check whether or not a string contains a substring 

```
const str = 'what up';
console.log(str.includes('what')); // true
console.log(str.includes('down')); // false 
```

#### match(regex string)
- checks if a string matches a regular expression 

```
const firstName = "Matt";
const badFirstName = "Matthew4";
const nameRegex = /^[a-zA-Z]+$/ 

console.log(firstName.match(nameRegex)); // true
console.log(badFirstName.match(nameRegex)); // false 
```

#### replace(stringToBeReplaced, stringToAdd) 
- takes an occurence of a character in a string and replaces it with another character 

```
const userInput = '917 716 4543'; 
console.log(userInput.replace(' ', '-')); // 917-716-4543
```

#### split(string)
- return an array of substrings when needing to split a string 

```
const seeyou = "See You"; 

const seeYouSplit = see.split(' '); 
console.log(seeYouSplit); // ["See", "You"]; 
console.log(seeYouSplit[0]); // "See"
```

#### substring(index, index)
- when needing to split a string at a certain index or range of indices, you pass this function in the index of the element you want to start at, as well as the index in the string you want the substring to end at 

```
const goodbye = 'Goodbye Friend'; 
console.log(goodbye.substring(1, 4); // ood
```

 
#### toLowercase()/toUppercase()
- used to make sure the string isn't case-sensitive 

```
const firstName = "Yani"; 
console.log(firstName.toUpperCase()); // YANI
```

#### trim() 
- removes whitespace from any string 

```
const strWithSpace = 'Yani '; 
console.log(strWithSpace.trim()); // outputs 'Yani' 
```


###Conclusion 
The main take away from this blog is that it's always smart to strengthen your foundational knowledge first before embarking on more complex topics like data structures and algorithms. I hope this blog was helpful!