0

[JS] What's the difference between "split", "trim" And "slice" And how do I properly use each one?

So I've seen codes where it has something like input.slice(3).trim().split(' '); And I don't know what each does :(

6th Dec 2018, 12:30 PM
Joe Smith
Joe Smith - avatar
5 Respuestas
+ 8
Here slice(3) will remove first 3 characters of string trim() will remove the black spaces at start and end of string split(' ') will split the string according to delimiter specified in the brackets I.e here ' ' example string is "Hey Rst ar " slice(3) " Rst ar " trim() "Rst ar" split(' ') ["Rst" , "ar"]
6th Dec 2018, 12:42 PM
Rstar
Rstar - avatar
+ 6
"Joe do, stuff please !!" slice() " do, stuff please !!" trim() "do, stuff please !!" split() [ "do," ,"stuff", "please","!!" ]
6th Dec 2018, 12:58 PM
Rstar
Rstar - avatar
+ 6
You can always check on Sololearn https://code.sololearn.com/W537ta9ICLgL/?ref=app
6th Dec 2018, 1:02 PM
Rstar
Rstar - avatar
+ 3
The trim() method removes whitespace from both sides of a string. The split() method is used to split a string into an array of substrings, and returns the new array. The slice() method extracts parts of a string and returns the extracted parts in a new string. Referenced: https://www.w3schools.com/jsref/jsref_slice_string.asp
6th Dec 2018, 12:47 PM
ODLNT
ODLNT - avatar
+ 1
Can I get an extra example that would clarify more, lets say my input is "Joe do, stuff please !!" How would that work for each one then how would it work if I slice, trim and split all together
6th Dec 2018, 12:54 PM
Joe Smith
Joe Smith - avatar