DEV Community

ahmadullah
ahmadullah

Posted on

JavaScript string methods #2

In the name of Allah
As we studied yesterday about indexOf() method and bracket notation in javascript today I want to teach something different that's a whole sentence that we can find separate characters index.
Here is a demo of it:

let greeting='Hello how are you';
//now want to find the index of how
//look when we want to find the index of how the indexOf() method //returns the first index of the string it means return the index //of h which is the first character in how string.
console.log(greeting.indexOf('how'));
//result -> 6
Enter fullscreen mode Exit fullscreen mode

*Notice whitespace behaves as character.
Look at this example

let greeting='Hello how are you';
console.log(greeting.indexOf(' '));
//result -> 5
Enter fullscreen mode Exit fullscreen mode

How to check if a sentence contains a character

we can use indexOf() method to find if a character is available or used in our sentence.
*Notice when a character is not available in our sentence the indexOf() method returns -1 which denotes that that character isn't used in your sentence.
we can use indexOf() method to check whitespace in our password or something else.
Here is a demo we check if whitespace is available in our password or not.

let password='ehgjt ertr94';
console.log(password.indexOf(' '));
//result -> 5
Enter fullscreen mode Exit fullscreen mode

as we see whitespace is in our password.
Here is the second password that there isn't any whitespace in our password.
Here is the demo:

let password='1234fjgjdlf';
console.log(password.indexOf(' '));
//result -> -1;
Enter fullscreen mode Exit fullscreen mode

which implies there isn't whitespace in your password.
we can as well use length() method to check the length of our password or username. I will teach you this trick when we reach conditional statements in Javascript.

split(separator: string | RegExp, limit?: number)

This method converts a string into an array or list.
The separator means that from which character I should split that string into an array and the second part means how many characters you want to be in your list.
As you see there is Regex which is a big title we will teach you.
Here is a demo of my claim:

let introduction='Hello my name is ahmad';
let stringList=introduction.split(' ');
// I targetted the whitespace it means my string should be split when the split method finds whitespace and the second part is optional which is the number of character or string to be in your list
console.log(stringList);
//result ->['Hello','my','name','is','ahmad'];
Enter fullscreen mode Exit fullscreen mode
  • we can use any other character as a separator.

Here is an example:

let greeting='my,name,is,kamal';
let stringList=greeting.split(',');
//here i targetted ',' as a separator.
console.log(stringList);
//result -> ['my','name','is','kamal']
Enter fullscreen mode Exit fullscreen mode

you can experiment more on split() method.
Tomorrow I will teach you more about split() method.
See you soon.

Top comments (0)