Split the string into substrings using delimiter | function to split string into substrings on the basis of delimiter and return the substrings after split ; to count the number of split strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty string , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted strings ; Driver program to test above
function splitStrings ( str , dl ) { var word = " " ; var num = 0 ; str = str + dl ; var l = str . length ; var substr_list = [ ] ; for ( var i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) word = word + str [ i ] ; else { if ( word . length != 0 ) substr_list . push ( word ) ; word = " " ; } } return substr_list ; } var str = " " ; var dl = ' ' ; var res = splitStrings ( str , dl ) ; res . forEach ( x => { document . write ( x + " " ) ; } ) ;