Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

String methods and properties

Strings can have methods and properties exactly like objects. 

One of the commonest properties is the length property who returns the length of a string and how many characters this string is comprised of.

Another important method is the  split method. That splits actually a string into a smaller strings, and then it puts them as elements into an array. The new array is comprised of string elements and you can work individually with every substring. Split is a method, that means that you can also pass an argument and specify every WHICH character occurrence you want  to split your main string.

For example if you have a string like: let sentence = 'this sentence is nonsense',

sentence.split('') // Splits every single character into an array. ['t', 'h', 'i', 's', ' ', 's', 'e', 'n', 't'] etc.

sentence.split(' ') (with a space inside the argument. Splits every time it finds a space. So we end up with something like. ['this', 'sentence', 'is', 'nonsense'].

 

Find out all methods for strings here.

The length property and the split method

Kostas Diakogiannis
Module by Kostas Diakogiannis, updated more than 1 year ago
No tags specified