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 :(
5 odpowiedzi
+ 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"]
+ 6
"Joe do, stuff please !!"
slice() " do, stuff please !!"
trim() "do, stuff please !!"
split() [ "do," ,"stuff", "please","!!" ]
+ 6
You can always check on Sololearn
https://code.sololearn.com/W537ta9ICLgL/?ref=app
+ 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
+ 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