DEV Community

Cover image for String & Array Methods I recently learned!
Ellaine Tolentino
Ellaine Tolentino

Posted on

String & Array Methods I recently learned!

Hi y'all! Below are my most recent discovery of some string and array manipulation methods! I stumbled upon these methods while I was working through my daily algos. I can't pass up the chance to share what I learned! So, here it goes!

fill()

It fills up an array of undefined elements.
fill method applied


repeat()

Takes a non-negative argument to determine the times of repetition and concats everything into one string. It is non-destructive and only makes a copy of the original string argument.
Repeat method applied

Using string literals
Using string literals or template literals

repeat with float num
Float number arguments will be considered as integers.


fromCodePoint()

Static string method. Can't be used for a string object you created. Can be used for symbol matching

Syntax:

String.fromCodePoint(9731) - '' (_Yes I think it's a snowman too_)
Enter fullscreen mode Exit fullscreen mode

codePointAt()

You're maybe wondering how would you know which code point to use on the method beforehand. This is it!
This method returns a integer(non-negative) that is UTF-16 code point value. Takes in the index/position of the character from the string you want to know the code point value of.

Syntax:

let a = '★♲☃'
console.log(a.codePointAt(0))
//9733 
Enter fullscreen mode Exit fullscreen mode
I think this can work really well in validations or comparison if needed.

padEnd()

Yes! It pads the end of the string to reach the length you indicated in the argument.

Syntax:

let str = "Yup"
console.log(str.padEnd(8)+ "*")
// Yup     *
Enter fullscreen mode Exit fullscreen mode

It can also take in a 2nd argument if you have a specific character or string to repeatedly pad your object.

Syntax:

let str = "Yup"
console.log(str.padEnd(8, ".")+ "!")
// Yup.....!
Enter fullscreen mode Exit fullscreen mode

Same principle goes for padStart()


trim(), trimEnd(), trimStart()

Removes whitespaces from both ends of the string. While trimEnd and trimStart removes whitespaces from a specific side of the string.

trimEnd() & trimStart() are also known as trimLeft() & trimRight().

You maybe wondering in what algorithm was I able to use some of these methods?

Try and see if you can solve this Codewars Credit card mask challenge using some of the methods above!

Another one you can try is from CodeSignal, called Frame Generator. The goal is to write a function that takes in a number and returns an array of strings that would look like frame of asterisks.

function call
Check out how I utilized some methods above to solve this! - Github.com/tolentinoel

Hope this is trivial! Until the next!

Top comments (0)