How to make string from array
How to make string from array
JavaScript array to string (with and without commas)
Posted on Jan 14, 2021
Let’s learn how to convert JavaScript array to string with or without commas. Example code included.
The method will return a string that represents the elements stored in your array:
Keep in mind that the toString method can’t be used on an array of objects because it will return [object Object] instead of the actual values.
To convert an array of objects into a string, you need to use the JSON.stringify method:
It’s not really a representation of the array values because the return value will still have the square and curly brackets, but it’s the best you can do with an array of objects.
JavaScript array to string without commas
Sometimes you may need to convert your array into a string without the commas. The toString method has no parameter, so you need to use join method instead.
join accepts one string parameter which will serve as the separator for your string:
You can even pass an empty string to the method:
And that’s how you turn a JavaScript array into a string.
Level up your programming skills
I’m sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Nathan Sebhastian is a software engineer with a passion for writing tech tutorials.
Learn JavaScript and other web development technology concepts through easy-to-understand explanations written in plain English.
Convert javascript array to string
I’m trying to iterate over a «value» list and convert it into a string. Here is the code:
17 Answers 17
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If value is not a plain array, such code will work fine:
(output will appear in the dev console)
As Felix mentioned, each() is just iterating the array, nothing more.
That’s it Now A is a string. 🙂
not sure if this is what you wanted but
This results in the following output:
Four methods to convert an array to a string.
Coercing to a string
You can use other separators, for example, ‘, ‘
This is cleaner, as it quotes strings inside of the array and handles nested arrays properly.
But why use jQuery at all in this case? map only introduces an unnecessary function call per element.
∆: It only uses the return value whether it should continue to loop over the elements or not. Returning a «falsy» value will stop the loop.
int array to string
In C#, I have an array of ints, containing digits only. I want to convert this array to string.
13 Answers 13
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
at.net 4.0 or above use: (see @Jan Remunda’s answer)
You can simply use String.Join function, and as separator use string.Empty because it uses StringBuilder internally.
It actually works with null separator, and second parameter can be enumerable of any objects, like:
I realize my opinion is probably not the popular one, but I guess I have a hard time jumping on the Linq-y band wagon. It’s nifty. It’s condensed. I get that and I’m not opposed to using it where it’s appropriate. Maybe it’s just me, but I feel like people have stopped thinking about creating utility functions to accomplish what they want and instead prefer to litter their code with (sometimes) excessively long lines of Linq code for the sake of creating a dense 1-liner.
I’m not saying that any of the Linq answers that people have provided here are bad, but I guess I feel like there is the potential that these single lines of code can start to grow longer and more obscure as you need to handle various situations. What if your array is null? What if you want a delimited string instead of just purely concatenated? What if some of the integers in your array are double-digit and you want to pad each value with leading zeros so that the string for each element is the same length as the rest?
Taking one of the provided answers as an example:
If I need to worry about the array being null, now it becomes this:
If I want a comma-delimited string, now it becomes this:
This is still not too bad, but I think it’s not obvious at a glance what this line of code is doing.
Of course, there’s nothing stopping you from throwing this line of code into your own utility function so that you don’t have that long mess mixed in with your application logic, especially if you’re doing it in multiple places:
But if you’re going to put it into a utility function anyway, do you really need it to be condensed down into a 1-liner? In that case why not throw in a few extra lines for clarity and take advantage of a StringBuilder so that you’re not doing repeated concatenation operations:
And if you’re really so concerned about performance, you could even turn it into a hybrid function that decides whether to do string.Join or to use a StringBuilder depending on how many elements are in the array (this is a micro-optimization, not worth doing in my opinion and possibly more harmful than beneficial, but I’m using it as an example for this problem):
For this example, the performance impact is probably not worth caring about, but the point is that if you are in a situation where you actually do need to be concerned with the performance of your operations, whatever they are, then it will most likely be easier and more readable to handle that within a utility function than using a complex Linq expression.
That utility function still looks kind of clunky. Now let’s ditch the hybrid stuff and do this:
Now we have separate and fairly compact utility functions, each of which are arguable useful on their own.
Ultimately, my point is not that you shouldn’t use Linq, but rather just to say don’t forget about the benefits of creating your own utility functions, even if they are small and perhaps only contain a single line that returns the result from a line of Linq code. If nothing else, you’ll be able to keep your application code even more condensed than you could achieve with a line of Linq code, and if you are using it in multiple places, then using a utility function makes it easier to adjust your output in case you need to change it later.
For this problem, I’d rather just write something like this in my application code:
Converting Arrays to Strings in JavaScript
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Arrays are a powerful feature of any language. They let you house multiple pieces of information easily. They will maintain the order of items being added and can even be sorted. In modern web browsers, they even handle auto-magically converting themselves into human readable strings.
Even with the modern niceties, it’s still good to know how to convert an array to a string for those times when you need to massage an array into another format, or want to do something more than simply comma separating the values (the default behavior).
Getting Started
No special tools required. You can hack along in either your web browser’s console or using the Node.js REPL. The commands utilized are baked into the Array Object’s prototype.
To string, or Not to String.
If you were to run the following in your browser:
You would receive an alert with the message 1,2,3 without any additional work on your part. If all you need to do is display the contents of an array in this format, this can get you pretty far. This even works when referencing array objects inside of template literals.
Both result in the same 1,2,3 string.
Join Together
Prefer to have each array item on it’s own line? Pass in a new line character:
Working with HTML and want to use those breaks instead?
Want HTML breaks AND new lines. Okay, you get the idea.
Speaking of HTML
Still pretty handy for dealing with multiple line strings and/or markup when you don’t have access to some of the more modern syntax options.
Conclusion
Working with a back-end server that doesn’t understand arrays being passed-in? Join your array items into a single value and you’re good to go!
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
how can i convert an int array into a string array [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
I have an integer array:
I need to store these number into char array so that the char array will have some thing like this:
Is there any library function in c for this?
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
sprintf do what you need.
snprintf takes care of the str length
What you need to do is to convert the integer values in a into character values in s leaving room for a null-terminating character. In your case a total of 9 characters. sprintf is the proper tool since vales exceed single digits:
Output
Tried to go for a more readable approach even though the other answers are more efficient in terms of memory and instructions.
You can search for sprintf() / snprintf() which can get the job done for you.
From the man page,
The functions in the printf() family produce output according to a format as described below.. [. ].
Источники информации:
- http://stackoverflow.com/questions/5289403/convert-javascript-array-to-string
- http://stackoverflow.com/questions/1822811/int-array-to-string
- http://www.digitalocean.com/community/tutorials/js-converting-arrays-to-strings
- http://stackoverflow.com/questions/30234363/how-can-i-convert-an-int-array-into-a-string-array