JavaScript Tutorial For Beginners #25 - Slice and Split Strings

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024
  • Yo ninjas, I just want to show you a couple of neat little string methods (functions) in this JavaScript tutorial. The slice method and the split method. The slice method literally 'slices' your string into a new segment defined by starting and ending points. The split method, splits a string into multiple 'pockets' and puts them into an array (great for splitting tags).
    As always, any questions, fire away below :).
    SUBSCRIBE TO CHANNEL - / @netninja
    ========== JavaScript for Beginners Playlist ==========
    • JavaScript Tutorials f...
    ========== CSS for Beginners Playlist ==========
    • CSS Tutorials For Begi...
    ========== HTML for Beginners Playlist ==========
    • HTML Tutorials For Beg...
    ========== The Net Ninja ============
    For more front-end development tutorials & to black-belt your coding skills, head over to - / @netninja or thenetninja.co.uk
    ========== Social Links ==========
    Twitter - @TheNetNinja - / thenetninjauk

Комментарии • 42

  • @AllenZitting
    @AllenZitting 6 лет назад +20

    I love how short and simple these vids are. Thanks

  • @TheHappyKamper
    @TheHappyKamper 3 года назад +1

    I actually was planning to use split, so searched that in RUclips. But while watching this video, I realised slice is more fitting for my purpose. Cheers!

  • @etymol8896
    @etymol8896 7 лет назад +20

    This tutorial made me hungry for prok~!
    But, f'reals though, you and the tutorials you make are awesome!
    A very nice balance of thorough, to-the-point lecturing, and light-hearted feels.
    Please keep doing what you do!

  • @aqeemjones
    @aqeemjones 3 года назад +4

    var spy = "I was loving you"
    var spy2 = spy.slice(0);
    var spy3 = spy2.slice(6);
    document.write(spy3 + " daily NetNinja");

    • @A-Miracle000
      @A-Miracle000 2 года назад +2

      var spy = "I was loving you";
      console.log(spy.slice(6) + " daily NetNinja");
      // much easier :D

  • @cristianfz16
    @cristianfz16 4 года назад +1

    This channel is so great, you explain so simple and nietly!

  • @Elizabeth-vl5lb
    @Elizabeth-vl5lb 7 лет назад +3

    the best explanation of arrays

  • @clintona1855
    @clintona1855 4 года назад +1

    I like how you just jump into the subject matter.

  • @toxxicpit
    @toxxicpit 5 лет назад +3

    sure feeling like having a little prok right now!

  • @mrcoder7327
    @mrcoder7327 5 лет назад +1

    Im eating a cake while watching this video and I can really taste the layer! Okay i'll stop...

  • @user-uf3lx7rj2k
    @user-uf3lx7rj2k 2 года назад

    Thank you man for the help

  • @raphaelchagas1676
    @raphaelchagas1676 3 года назад

    It really helped. Thanks

  • @kennethscribner6673
    @kennethscribner6673 4 года назад

    Help a lot man. Thx

  • @shivankmittal8521
    @shivankmittal8521 2 года назад

    when we put slice(2,9) then why 2nd digit is included and 9th one is not to be. In str2.

  • @gokulk9247
    @gokulk9247 7 лет назад +2

    is the mozilla page link in description missing.

    • @upasanamnnit
      @upasanamnnit 7 лет назад

      Here is the link-
      developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

  • @zoecarlibur
    @zoecarlibur 7 лет назад

    Awesome, thanks for sharing.

  • @medamiinlechlech7031
    @medamiinlechlech7031 7 лет назад

    You save me

  • @ChristianCanlubo
    @ChristianCanlubo 6 лет назад

    Cannot read property 'slice' of null
    this is printing on my developers tool

  • @ashishupadhyay5407
    @ashishupadhyay5407 3 года назад

    this video was uploaded on my birthdate. nice

  • @BlueSky-hd6lq
    @BlueSky-hd6lq 5 лет назад

    thank you bro

  • @raniaghatesse3278
    @raniaghatesse3278 5 лет назад

    so helpfull

  • @user-zi3qt9kb2q
    @user-zi3qt9kb2q Год назад

    var t="rup,vars,abhi,sai,ana" this is the statment i wrote
    i got 'rup,vars,abhi,sai,ana'(getting in single quotes ' ')
    hope i will get the answer.

  • @harishtg2877
    @harishtg2877 4 года назад

    Can some one guide me how can i slice a specific string of array which includes number in it

  • @chumberjosh
    @chumberjosh 5 лет назад

    how would you do this with shapes

  • @tamasdenes1714
    @tamasdenes1714 7 лет назад

    Hi, and what is when i want to slice the string from backwards?

    • @bassdewd
      @bassdewd 7 лет назад +3

      str.slice(-6); // Finds the last 6 characters.
      OR
      str.slice(-6, -2); // Finds the last 6 characters except the last 2.

  • @BennyPowers
    @BennyPowers 8 лет назад

    How can we strip out the leading spaces from array positions >0?

    • @NetNinja
      @NetNinja  8 лет назад

      +Benny Powers Hey, on a string, to strip out the spaces, you can use the replace method. On arrays, you could use a for loop to cycle through and remove any empty elements.

    • @briantoon4128
      @briantoon4128 6 лет назад +2

      use comma and space in quotations
      .split(", ")
      instead of just comma in quotations
      .split(",")

  • @kalamashaka8280
    @kalamashaka8280 6 лет назад

    HEY NET NINJA, how do i split let alphabet = "abcdefghijklmnopqrstuvwxyz" into a single character with space [let alphabet1= alphabet.split(" ") ; ] is not working

    • @ukaszcezary8870
      @ukaszcezary8870 6 лет назад

      Hello
      Nelson Ongiti
      ,
      you can do it something like that:
      var alphabet = "abcdefghijklmnopqrstuvwxyz";
      var alphabetSplit = alphabet.match(/.{1}/g);
      console.log(alphabetSplit);

    • @oufcena5092
      @oufcena5092 4 года назад

      @@ukaszcezary8870 Better simple solution
      const alphabet = "abcdefghijklmnopqrstuvwxyz";
      const alphabetSplit = alphabet.split("");
      console.log(alphabetSplit);

    • @oufcena5092
      @oufcena5092 4 года назад

      const alphabet = "abcdefghijklmnopqrstuvwxyz";
      const alphabetSpacedSplit = alphabet.split("").map(value => value + " ").join("").trim();
      console.log(alphabetSpacedSplit);

  • @teddybear4466
    @teddybear4466 5 лет назад

    Hello,
    how can I split a string without using the split function? What is the basic function for it?

  • @batumanav
    @batumanav 3 года назад +1

    16 dislikers are the ones who split strings to "llo, wo"

  • @anniekhaliq6215
    @anniekhaliq6215 8 лет назад

    what about negative slice numbers?

    • @NetNinja
      @NetNinja  8 лет назад +1

      +annie khaliq Hey :). Using negative numbers, we'd just start at the end position of the array/string and count from there.

    • @nadiacarmon932
      @nadiacarmon932 6 лет назад

      If var stuff = "pencil?", then stuff.slice(-5) = "ncil?". Basically, it will return all characters leading up to that number, but in reverse. stuff.slice(5) = "penci".
      It's similar to the .length property.
      Stuff.length - 1 gives you the last character of the string, just like stuff(-1) will also return the last letter.