Let's get to it!
Let text = ‘Airbrake is Awesome’
Or you can put the exact same string in double-quotes:
Let text = “Airbrake is Awesome”
If you already know what an array is and you’re comfortable working with arrays, please proceed to the next section, where we’ll go over how to convert strings to arrays.
For those of you who might need a refresher on arrays, here’s a simple definition:
Here’s an example of an array:
Const A = [“Airbrake”, “ Error Monitoring”, “Debugging”]
You’ll receive the following list when you run this script:
Airbrake, Error Monitoring, Debugging.
And that’s an array!
Why Convert a String to an Array
When working with Data, there are times when we're in control and when we're not. By changing a string into an array, it allows us to take control of certain data points.
For example, maybe we have incorrectly formatted data coming from the API. Perhaps the API might contain a specific character for ‘Airbrake-is-awesome’ that is not required in the final output. For this purpose, we will split the string data by using a delimiter (which we'll go over in the next section) for every occurrence of “-” to get the final output “Airbrake is awesome.”
Or, perhaps, you have a list of strings that you want to call on as an array because, from a data point of view, it makes more sense.
As you can see, strings and arrays serve two different purposes. A string stores characters, while an array creates list-like objects due to its ability to hold more than one value. You might need to convert those strings into an array in some cases. The split() method is the most popular way to do this.
Using the Split() Method
Here's an example:
Let A = “Airbrake Error Monitoring and Debugger”;
Const split_string = A.split(“ “):
Output = [“Airbrake”, “Error”, “Monitoring”, “and”, “Debugger”]
What just happened here? Why did the A.split(“ “) turn our original string into an array?
This is how the computer program reads the split(): string.split(delimiter, limit)
Let's continue this demostration and use the same example. Let's say the data we get from an API looks like this “Airbrake-is-awesome,” and output should be a string “Airbrake is awesome.”
Here we use the string split() method with a separator “-”. The output is an array of strings separated at every instance of “-”. To get our desired string output, we will use the Array join() method to combine each element in the array back into a string without the “-” character.
What is a Delimiter and a Limit
In the above example, the string we converted into an array is the one set to “A” and between the double-quotes. You can take it a step further, as well. You can change the delimiter so the program picks up specific areas to convert into an array.
Let B = “Airbrake, Error, Monitoring, Performance, Debugger”;
Const split_string = B.split(“ , “):
Output = [“Airbrake”, “Error”, “Monitoring”, “Performance”, “Debugger”]
But, if you leave the delimiter empty, like so:
Let C = “Airbrake”;
Const split_string = C.split(“ “); //note that there is no additional space between “ “
The output is:
[“A”, “i”, “r”, ”b” ,”r”, ”a” ,”k”, ”e”]
What happens when you put a limit in? When you put a limit in, you’re limiting how much a string should be listed within your array.
Here’s an example of a limit with 6:
Let D = “Airbrake is an awesome error monitoring and debugging tool”;
Const split_string = D.split(“ “ ,6);
The output will be:
[“Airbrake”, “is”, “an”, “awesome”, “error”, “monitoring”].
The split() method is the best and most common method out there for converting a string into an array.
Errors When Converting From a String to an Array
When switching between strings and arrays, there’s always a chance that you might create an error that could end up in production. Here is a list of errors that might arise and how to fix them if they do:
"Object doesn't support this property or method" or “split is not a function”
"Split is Not a Function" Error
If this error pop-ups for you, it means that your attempt to split() failed and didn’t convert your string to an array. The easiest way to fix this is to return to the split() and fix what’s causing the error.
“TypeError: Cannot read property 'split' of undefined” Error
When using the string split() method, keep an eye out for TypeErrors. TypeErrors occur if the split string is "undefined."
Avoid this error by using proper error handling. Before involving the split() method, check for any undefined variables.
Find Errors With Airbrake
Give it a try! Create a free-forever developer account, which comes with unlimited error and performance events for the first 30 days.