Sitemap
Ashok Maharjan

Ashok Maharjan

Common String Methods

3 min readSep 26, 2020
Press enter or click to view image in full size
https://www.nicepng.com/ourpic/u2q8y3y3i1r5i1u2_string-of-christmas-lights-clipart-free-clipart-clip/

I’ve been job searching for a while now and have been presented with multiple coding challenges. Some of them were hard and some of them were easy but the common theme among them has been string and array manipulation. I have already written about common array methods in my earlier blog here, so today I will be exploring some useful string methods that I have been using frequently.

Get Ashok Maharjan’s stories in your inbox

Join Medium for free to get updates from this writer.

charAt(): It returns the single character that is located at the given index of the string. e.g.

includes(): It is used to check if a string is included in another string. The search will start from an index passed as second argument and if nothing is passed, it will start searching from index 0. e.g.

toUpperCase() and toLowerCase(): These methods are used to convert the given string into uppercase or lowercase respectively. e.g.

indexOf(): It is used to find the index of first occurrence of a character or a string pattern in the given string. A starting index can be provided as an argument similar to the includes() method. It returns -1, when there is not match. e.g.

replace() and replaceAll(): They are used to replace the sample string or a regular expression in the given string. Replace() operates on only the first occurrence and replaceAll() operates on all occurrence. e.g.

slice(): It returns the substring from the given string, extracted from provided starting index and optional ending index. We can also pass negative index which is used to count backwards. e.g.

Press enter or click to view image in full size

split(): It returns the array that is populated by the strings divided by a search pattern. e.g.

These are just a few string methods that I have been using frequently and to get the complete list of string methods please refer to the official documentation here. So that’s al for this edition of the blog and until next week happy coding.

--

--