Play Around With Strings - Javascript

Play Around With Strings - Javascript

Jul 10, 2020ยท

3 min read

A string is a data type that stores text enclosed within single or double quotes.

Initializing and assigning a value to a string.

var name = "faith2020";

Are strings in Javascript arrays?

No. Strings are not arrays. However, the items making up the string are indexed just like the items in an array. The string items are also accessed through bracket-notation just like in an array.

name[2]; returns "i" Notice that this returns a string.

Let's get dirty ๐Ÿถ

Enough with the niceties let's actually play around with our string and other strings that we will create along the way. We shall manipulate the strings using common string methods used in Javascript.

1. Convert a string into an array of its sub-strings

Consider the string: var description = "I like playing football"; This string can be split into its component substrings. For example; "I", "like", "playing", "football". Why would I separate the strings? You could possibly be wondering why we're separating the substrings. This is useful when one is working on just a single substring. This is in the instance of search and replace, changing the case of the given substring or modifying the substring.

To split a string we call the string method split(). This method returns the new array of substrings.

var name="faith"; var res=name.split(""); //returns ["f","a","i","t","h"]

var description="I like playing football"; var res=description.split(" "); //returns [ 'I', 'like', 'playing', 'football' ]

var description="I like playing football"; var res=description.split(); //returns [ 'I like playing football' ]

Notice the difference in the parameters passed in the split() method.

2.Bring back our split substrings into one string

After manipulating each of the substrings in the array, you may need to bring them back to a string and continue using them as a new full string. To do this we use the join() method. This method returns the array of substrings into a single string.

var res=[ 'I', 'like', 'playing', 'football' ]; var str=res.join(); //returns 'I,like,playing,football'

3. Extract part of a string

To extract part of a string use the slice() method. This method takes two parameters; what position to start extracting from and which position to stop extracting from.

var str = "football"; var res = str.slice(0,5); //returns 'footb'

This starts extracting from index 0 and stops at index 5. Note that index 5 is not included in the extracted string.

Pass only the starting position parameter if you'd like to extract to the end of the string.

var str= "football"; var res=str.slice(4); //returns 'ball'

4. Determine whether the string ends with a given value

We have the string "I love football" and we'd like to know whether it ends with the string football. To do this we use the endsWith() method. This method takes two parameters; the string we're searching for at the end and what length of the original string should we search. The second parameter is however optional.

var str="I love football"; var res=str.endsWith("football"); //returns true

The method returns either true or false.

5.Change the case of a string

We could change our string to uppercase or lowercase. To do this we use the toLowerCase() and toUpperCase() methods.

var str="faith"; var res=str.toUpperCase(); //returns 'FAITH'

var str="GAICIUMIA"; var res=str.toLowerCase();//returns gaiciumia

Note that both methods do not take any parameters.

After-Play Activities ๐Ÿ™Œ

๐Ÿ’ There are more String methods in Javascript; this article just discussed a few.

๐Ÿ’ I am glad you've read all through. Thank you!. Don't forget to leave feedback.

๐Ÿ’ If you like my content kindly follow me on Hashnode and on Twitter as well.

๐Ÿ’ This is part of my #2Articles1Week writing challenge. Thank you Hashnode!