4 don't you mean whitespace or comma? – KaptajnKold Apr 27 '12 at 7:52 ,Thanks for contributing an answer to Stack Overflow!,… \s* matches zero or more white space characters (not just spaces, but also tabs and newlines).,... [\s,] matches one white space character or one comma
String.split() can also accept a regular expression:
Search Answer Titles
var text = "hoi how are you"; var arr = text.split(/\s+/);
I would like to split a String but I would like to keep white space like:,You should also make sure it is followed by whitespace s. so that strings like "My car isn't red" still work:,For split string by space like in Python lang, can be used:,You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.
I would like to split a String but I would like to keep white space like:
var str = "my car is red"; var stringArray; stringArray = "my"; stringArray = " "; stringArray = "car"; stringArray = " "; stringArray = "is"; stringArray = " "; stringArray = "red";
The split() method splits a string into an array of substrings, and returns the new array.,If an empty string ("") is used as the separator, the string is split between each character.,Split a string into an array of substrings:,The split() method does not change the original string.
Definition and Usage
split() method splits a string into an array of substrings, and
returns the new array.
If separator is an empty string (""), str is converted to an array of each of its UTF-16 "characters". , When found, separator is removed from the string, and the substrings are returned in an array. , If the separator is an array, then that Array is coerced to a String and used as a separator. , An Array of strings, split at each point where the separator occurs in the given string.
split() split(separator) split(separator, limit)
separator (optional): the separator is a pattern that will be used to divide the string (e.g. blank spaces, semicolons, http:// etc.).,The separator parameter allows the use of regular expressions.,Tip: to edit out the blank spaces at the beginning of the sentences use the array iteration method map() to apply the trim() method to each sentence.,You can also pass an array as the value for the separator parameter. When doing so, the array is converted to a string before being used as a separator, as in the example below:
The code below demonstrates how to use the split() method:
const str = 'Form an array from this string'; const arrCopy = str.split(); console.log(arrCopy); // expected output: ["Form an array from this string"] const words = str.split(' '); console.log(words); // expected output: ["Form", "an", "array", "from", "this", "string"]
Method 1: Using split() and join() Method: The split() method is used to split a string into multiple sub-strings and return them in the form of an array. A separator can be specified as a parameter so that the string is split whenever that separator is found in the string. The space character (” “) is specified in this parameter to separate the string whenever a space occurs.,Method 2: Using replace() method with regex: The replace() method is used to replace a specified string with another string. It takes two parameters, first is the string to be replaced and the second parameter is the string replaced with. The second string can be given as empty string so that the empty space to be replaced.,The first parameter is given a regular expression with a space character (” “) along with the global property. This will select every occurrence of space in the string and it can then be removed by using an empty string in the second parameter. This will remove all the spaces in the original string.,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
string.replace(/ /g, "")
- Reactjs + typescript getting error: Type 'string' is not assignable to type 'never'
- Split one react component into two
- REACT: Updating one piece of data in a Firestore document results in all other data being overwritten with empty Strings
- Remove comma appearing before/after default value string in textarea
- React SSR error — Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
- Only show matching string in array when using 'useMemo' in react
- How to event listener or click listener in react js of dynamic string?
- Search a string having special character
- Chakra UI doesn't work with react-split-pane
- Swing method akin to HTML5's canvas.putImageData(arrayOfPixels, 0,0)
- Spotify Apps API: any more documentation?
- Detecting combination keypresses (Control, Alt, Shift)?
- Is Babel a compiler or transpiler?
- Get HTML content from another site
- How to delete an HTML element inside a div with attribute contentEditable?
- Fullscreenchange event not firing in Chrome
- Interrupting/stop a CSS3 transition on the actual position/state
- Using jspdf to save html page as pdf, saved pdf contains incomplete page content if browser is zoomed, Why?
- OnChange event using React JS for drop down
- JQuery focus() sometimes not working in IE8
- JQuery.on("drop") not firing
- Converting Unix timestamp to Date Time String [duplicate]