quinta-feira, 7 de dezembro de 2017

[English][Javascript]Using like a Java contains in Javascript

Some times we need to check a String content if have something that we need,
but in Javascript doesn't have a method as seen in Java or anothers programming languages
like "contains", but we be able to implement this, let me show:

//var with the content
var s = "some stuff";


/*
On this method, we check if the var have the indexOf greater than 0 if have, gotcha!
contains the text that wee searching inside, if the result is lower than, we doesn't have the result
*/

function contains(t, v){
 return t.indexOf(v) > -1;
}

for example:

var x = "Jhonatan";

contains(x, 'J')

Result : true

x = "Kerry";

contains(x, "J");

Result : false;




Nenhum comentário:

Postar um comentário