Reputation:
How do you check whether a character in a string is a specific character in JS? Currently I have code that checks each letter of a string and then goes through a huge if/else statement to check which letter it is, and I was wondering is there was a more efficient way to do this?
Example
var string = "hello"
Upvotes: 2
Views: 148
Reputation: 1337
There are many ways to accomplish this, you could for example have a series of if-else statements or a switch statement, I would suggest a different option though:
var str = 'hello',
actions = { // Define actions (function to call) you want for specific characters
h: function () {
// Do something if character was 'h'
console.log('h');
},
l: function () {
// Do something if character was 'l'
console.log('l');
},
o: function () {
// Do something if character was 'o'
console.log('o');
}
};
for (var i = 0; i < str.length; i++) {
if (actions[str[i]]) { // If there is an action/function defined for the current character then call the function
actions[str[i]]();
}
}
This you you don't have to "know" what character you are currently on in the loop, only if something should happen for it.
And for reference, achieving the same thing with if-else statements:
var str = 'hello';
for (var i = 0; i < str.length; i++) {
if (str[i] === 'h') {
// Do something if character was 'h'
console.log('h');
}
else if (str[i] === 'l') {
// Do something if character was 'l'
console.log('l');
}
else if (str[i] === 'o') {
// Do something if character was 'o'
console.log('o');
}
}
And with switch statement:
var str = 'hello';
for (var i = 0; i < str.length; i++) {
switch (str[i]) {
case 'h':
// Do something if character was 'h'
console.log('h');
break;
case 'l':
// Do something if character was 'l'
console.log('l');
break;
case 'o':
// Do something if character was 'o'
console.log('o');
break;
}
}
Upvotes: 1
Reputation: 48357
function do_something(str)
{
switch (str.substr(0,1).tolower()) {
case 'h':
// call function something_else with the remainder of the string
something_else(str.substr(1,str.length));
break;
case 'z':
another_thing();
break;
default:
// no need to explicitly add a case for 'h' - its handled here
break;
}
}
switch is a multi-way branch. There are other ways to slice up the string. In practice there is unlikely to be a noticeable difference in performance between using a case statement compared with a sequence of if...else if....else if
but it does make for better readability.
Some languages also provide constructs where you can define a routine to call at run time. This is also possible with javascript but makes it very easy to make bad mistakes and hard to debug/test/ them. The following is provided as an example of bad programming:
function fna()
{
...
}
function fnb()
{
...
}
...
function fnz()
{
...
}
var fn_to_call='fn' + str.substr(0,1).tolower() + '();';
eval(fn_to_call);
Upvotes: 0
Reputation: 428
Example with JS
Check if a string includes with "world":
var str = "Hello world, welcome to the universe.";
var n = str.includes("world");
The result of n will be:
true
The same applies to single chars in a single word
Credits: http://www.w3schools.com/jsref/jsref_includes.asp
Upvotes: 1