Controllare se una Stringa contiene una Sottostringa in JavaScript

Controllare se una stringa contiene o no una sottostringa è una delle cose più comuni che si possa fare nella programmazione.

In JavaScript ci sono vari metodi per fare ciò, ma uno dei più semplici è l’utilizzo del metodo includes(), introdotto con ES6/ES2015.

'Stringa di prova'.includes('stringa');  // true
'Stringa di prova'.includes('ciao');     // false

Questo metodo accetta anche un secondo parametro: un numero intero che può indicare da che posizione iniziare a cercare la stringa:

'Stringa di prova'.includes('prova');      // true
'Stringa di prova'.includes('prova', 15);  // false
'Stringa di prova'.includes('prova', 11);  // true

#Supporto dei Browsers

Il numero indica la versione minima del browser in cui la feature sia supportata. Per ottenere maggiori informazioni, passare con il mouse sopra la versione.

Desktop

chrome
Google Chrome
41
firefox
Mozilla Firefox
40
ie
Internet Explorer
No
edge
Microsoft Edge
12
opera
Opera
28
safari
Apple Safari
9

Mobile / Tablet

and_chr
Android Chrome
91
and_ff
Android Firefox
89
android
Android
91
samsung
Samsung Internet
4
op_mob
Opera Mobile
62
ios_saf
iOS Safari
9.0

Queste statistiche sul supporto da parte dei browser provengono da Caniuse, che potete visitare per informazioni più dettagliate.

#Alternative di includes prima di ES6

Prima dell’uscita di ES6, un metodo per cercare se una stringa contenesse o meno una sottostringa, era quello di utilizzare il metodo indexOf. Questo metodo restituisce -1 se la stringa non contiene la specificata, mentre se la sottostringa viene trovata restituisce l’indice del carattere a cui inizia la stringa.

'Stringa di prova'.indexOf('prova') !== -1;  // true
'Stringa di prova'.indexOf('prova', 15);     // false
'Stringa di prova'.indexOf('prova', 11);     // true

Commenti

  • Area 52 delta 8 carts
    # 20 Aprile 2021 alle 16:02

    You really make it appear really easy with your presentation but I
    in finding this matter to be really one thing that I believe I’d never
    understand. It sort of feels too complex and
    very broad for me. I am looking forward to your subsequent publish, I will attempt to get the hang of it!

    Area 52 delta 8 carts – Area 52 delta 8 carts

    Area 52 delta 8 carts – buy delta 8 THC area 52

    Area 52 delta 8 carts – delta 8 THC for sale area 52

    Area 52 delta 8 carts – delta 8 THC for sale area 52

    area 52 delta 8 THC products – delta 8 THC area 52

  • CBD gummies for pain
    # 20 Aprile 2021 alle 16:42

    I visit every day a few web pages and blogs to read articles or reviews, however this blog offers quality
    based posts.

    Take a look at my page … CBD gummies for pain

  • delta 8 thc carts
    # 20 Aprile 2021 alle 19:13

    Wow, superb weblog structure! How long have you ever been blogging for?
    you make blogging look easy. The total glance of your web site is
    great, let alone the content!

    my blog :: delta 8 thc carts

  • best delta 8 thc gummies
    # 20 Aprile 2021 alle 20:59

    Howdy! I’m at work surfing around your blog
    from my new iphone 3gs! Just wanted to say I love
    reading through your blog and look forward to
    all your posts! Carry on the outstanding work!

    Feel free to surf to my web blog; best delta 8 thc gummies

  • delta 8 thc for sale
    # 20 Aprile 2021 alle 22:45

    Excellent website. A lot of helpful information here.

    I’m sending it to several friends ans also sharing in delicious.
    And certainly, thank you for your effort!

    Feel free to visit my web-site – delta 8 thc for sale

  • delta 8 thc
    # 21 Aprile 2021 alle 02:06

    Oh my goodness! Amazing article dude! Thanks, However I am
    going through issues with your RSS. I don’t know why I am unable to subscribe to it.

    Is there anybody having identical RSS issues? Anyone that knows the answer can you kindly respond?
    Thanx!!

    Feel free to visit my web page: delta 8 thc

  • buy cbd
    # 21 Aprile 2021 alle 09:36

    Hello There. I found your blog using msn. This is an extremely well written article.
    I will make sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will definitely return.

    My blog buy cbd

  • CBD gummies for sale
    # 21 Aprile 2021 alle 21:55

    Sweet blog! I found it while surfing around on Yahoo News.

    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Many thanks

    my site CBD gummies for sale

  • cbd for sale
    # 21 Aprile 2021 alle 22:28

    What’s up it’s me, I am also visiting this web site regularly,
    this web page is truly fastidious and the users are
    really sharing fastidious thoughts.

    Also visit my web site; cbd for sale

  • where to buy CBD
    # 23 Aprile 2021 alle 01:06

    Hi it’s me, I am also visiting this site daily,
    this web page is really good and the viewers are truly sharing fastidious thoughts.

    Also visit my page: where to buy CBD

  • cbd gummies
    # 23 Aprile 2021 alle 11:18

    Definitely believe that which you stated.

    Your favorite reason appeared to be on the net the
    easiest thing to be aware of. I say to you, I certainly
    get irked while people consider worries that they just do not know about.
    You managed to hit the nail upon the top and defined out the whole thing without having side
    effect , people can take a signal. Will probably be back
    to get more. Thanks

    My blog cbd gummies

  • delta 8 THC area 52
    # 23 Aprile 2021 alle 16:53

    Do you have any video of that? I’d like to find out some additional information.

    Feel free to visit my homepage … delta 8 THC area 52

  • buy instagram followers
    # 23 Aprile 2021 alle 22:13

    Wow! In the end I got a webpage from where I be able to
    actually obtain useful facts regarding my study and knowledge.

  • gold bee
    # 24 Aprile 2021 alle 01:25

    Wow, that’s what I was seeking for, what a information! existing here
    at this blog, thanks admin of this site.

    my site; gold bee

  • best CBD gummies
    # 24 Aprile 2021 alle 07:31

    A motivating discussion is definitely worth comment.
    I believe that you need to write more about this subject
    matter, it may not be a taboo subject but usually people don’t discuss these subjects.
    To the next! All the best!!

    my site – best CBD gummies

  • buy delta 8 THC area 52
    # 25 Aprile 2021 alle 04:16

    Excellent beat ! I would like to apprentice at the same time as you amend
    your web site, how can i subscribe for a weblog site? The account helped me a appropriate
    deal. I had been tiny bit acquainted of this your broadcast offered brilliant transparent concept

    Feel free to surf to my homepage … buy delta 8 THC area 52

  • delta 8 THC area 52
    # 25 Aprile 2021 alle 08:13

    I believe this is one of the most significant information for me.

    And i’m glad reading your article. However wanna statement on few normal things, The web site style is wonderful,
    the articles is actually excellent : D. Excellent activity, cheers

    delta 8 THC area 52 – delta 8 THC area 52

    delta 8 THC area 52 – Area 52 Delta 8 THC

    delta 8 THC for sale area 52 – delta 8 carts Area 52

    delta 8 carts Area 52 – Area 52 Delta 8 THC

    area 52 delta 8 THC products – Area 52 Delta 8 THC

  • buy instagram followers
    # 25 Aprile 2021 alle 09:10

    each time i used to read smaller content that also clear their motive, and that is also happening with this article which
    I am reading now.

  • delta 8 area 52
    # 25 Aprile 2021 alle 19:10

    Great blog right here! Additionally your web site lots up very fast!
    What host are you the usage of? Can I get your associate hyperlink in your host?
    I desire my site loaded up as fast as yours lol

    delta 8 area 52 – delta 8 area 52

    delta 8 area 52 – delta 8 THC for sale area 52

    Area 52 Delta 8 THC – Area 52 Delta 8 THC

    delta 8 THC for sale area 52 – delta 8 THC for sale area 52

    Area 52 Delta 8 THC – area 52 delta 8 THC products

  • delta 8 THC for sale area 52
    # 26 Aprile 2021 alle 07:21

    Hey I am so glad I found your blog, I really found
    you by mistake, while I was researching on Google for something else, Anyhow I am
    here now and would just like to say thanks for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to look
    over it all at the moment but I have bookmarked it and also added your RSS feeds,
    so when I have time I will be back to read more, Please do keep
    up the great job.

    Here is my website delta 8 THC for sale area 52

  • best CBD
    # 26 Aprile 2021 alle 19:35

    Wonderful beat ! I wish to apprentice while you amend
    your site, how can i subscribe for a blog website? The account aided me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright
    clear concept

    Feel free to surf to my web-site: best CBD

  • delta 8 THC area 52
    # 27 Aprile 2021 alle 00:31

    I’m now not positive the place you are getting your information,
    however great topic. I needs to spend some time studying more or working out more.

    Thanks for fantastic info I used to be in search of this info for my mission.

    Also visit my blog post … delta 8 THC area 52

  • buy instagram followers
    # 27 Aprile 2021 alle 00:57

    Hmm is anyone else encountering problems with the
    pictures on this blog loading? I’m trying to find out if its a problem
    on my end or if it’s the blog. Any feedback would be
    greatly appreciated.

  • CBD gummies for sale
    # 27 Aprile 2021 alle 01:10

    My spouse and I stumbled over here coming from a different web page and thought
    I may as well check things out. I like what I see
    so now i am following you. Look forward to going over your web page yet again.

    Visit my page; CBD gummies for sale

  • delta 8 THC for sale area 52
    # 27 Aprile 2021 alle 04:42

    That is a great tip particularly to those fresh to the blogosphere.

    Short but very accurate info… Thanks for sharing this one.
    A must read article!

    Feel free to visit my page – delta 8 THC for sale area 52

  • buy instagram followers
    # 28 Aprile 2021 alle 11:02

    Nice blog here! Also your site loads up very fast!

    What host are you using? Can I get your affiliate link to
    your host? I wish my site loaded up as quickly as yours lol

  • buy instagram followers
    # 30 Aprile 2021 alle 08:02

    Quality content is the secret to be a focus
    for the people to go to see the web site, that’s what this website is providing.

    P.S. If you have a minute, would love your feedback on my new
    website
    re-design. You can find it by searching for «royal cbd»
    — no sweat if you can’t.

    Keep up the good work!

    Here is my web page – buy instagram followers

  • casino utan svensk licens
    # 15 Maggio 2021 alle 01:51

    Very shortly this website will be famous among all blog users, due to it’s good
    articles or reviews

    Also visit my blog post – casino utan svensk licens

  • https://www.sfweekly.com
    # 3 Giugno 2021 alle 16:52

    It’s awesome to go to see this web page and reading the views of all colleagues about this piece of writing,
    while I am also keen of getting knowledge.

    my site – Best Delta 8 THC Gummies (https://www.sfweekly.com)

  • slot
    # 3 Giugno 2021 alle 19:39

    Very good site you have here but I was curious about if
    you knew of any message boards that cover the same topics talked about in this
    article? I’d really love to be a part of community where I can get advice from other knowledgeable people that share the same interest.
    If you have any recommendations, please let me know. Bless you!

    my blog … slot

  • best delta 8 thc gummies
    # 3 Giugno 2021 alle 21:21

    Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get
    a lot of spam comments? If so how do you
    reduce it, any plugin or anything you can recommend? I get so much lately it’s driving me crazy so any assistance is very much appreciated.

    Also visit my site … best delta 8 thc gummies

  • where to buy delta 8 gummies near me
    # 3 Giugno 2021 alle 23:03

    I’m amazed, I must say. Seldom do I come across a blog that’s both equally educative and engaging, and without a doubt, you have
    hit the nail on the head. The issue is an issue that too
    few folks are speaking intelligently about. I am very happy that I stumbled across this
    during my hunt for something concerning this.

    Here is my web site … where to buy delta 8 gummies near me

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *