![]() ![]() Building a compose function with reduce: const compose = (.fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value) Īnd now using it to compose splitByTilde and first functions. undefined: slice begins from index 0 > 0: zero-based index at which to begin < 0: an offset from the end of the sequence end ( option ). So we can compose those functions to build our final getName function. The algorithm is: split by the colon and then get the first element of the given list. Let's build a first function: const first = (list) => list To get the first element we can use the list operator. Example: splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ![]() So now we can use our specialized splitByTilde function. The string.slice() function is used to extract the parts of a string between the given parameters. Method 3: Using the slice() method to remove a character from a string. We want to make this "john smith~123 Street~Apt 4~New York~NY~12345" into this const split = (separator) => (text) => text.split(separator) You can see that using the regular expression and replace() met, we have replaced D character with nothing, which has removed the character from the string. So the first thing would be the split function. This string.split("~") gets things done.Īnother functional approach using curry and function composition. javascript - How do I split a string, breaking at a particular character - Stack Overflow How do I split a string, breaking at a particular character Ask Question Asked 14 years, 9 months ago Modified 8 months ago Viewed 1. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |