Convert Array of Integers to Comma-Separated String

Convert array of integers to comma-separated string

var result = string.Join(",", arr);

This uses the following overload of string.Join:

public static string Join<T>(string separator, IEnumerable<T> values);

Convert int[] to comma-separated string

Here's a stream version which is functionally equivalent to khelwood's, yet uses different methods.

They both create an IntStream, map each int to a String and join those with commas.

They should be pretty identical in performance too, although technically I'm calling Integer.toString(int) directly whereas he's calling String.valueOf(int) which delegates to it. On the other hand I'm calling IntStream.of() which delegates to Arrays.stream(int[]), so it's a tie.

String result = IntStream.of(intArray)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));

Convert a list of integers into a comma-separated string

Yes. However, there is no Collectors.joining for a Stream<Integer>; you need a Stream<String> so you should map before collecting. Something like,

System.out.println(i.stream().map(String::valueOf)
.collect(Collectors.joining(",")));

Which outputs

1,2,3,4,5

Also, you could generate Stream<Integer> in a number of ways.

System.out.println(
IntStream.range(1, 6).boxed().map(String::valueOf)
.collect(Collectors.joining(","))
);

How to convert array of Integers into comma separated string

In declaring GoalIds as an Array type, you are not getting an iterator to be able to run in String.Join.

Try:

int[] GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
var result = string.Join(",", GoalIds);

As @JeppeStigNielsen notes in the comments, this is also valid and eliminates the ToArray call:

var GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct();

How to convert array into comma separated string in javascript

The method array.toString() actually calls array.join() which result in a string concatenated by commas. ref

var array = ['a','b','c','d','e','f'];document.write(array.toString()); // "a,b,c,d,e,f"

Convert array of string or integers to comma separated integer or string for sql in condition

before calling getCategory() you create logicalUnitIdent String and pass. But modify the query to "in("+logicalUnitIdent+")"

logicalUnitIdent should be "1,2,3" or "'string1','string2','string3'
To convert List of string to single quoted and comma separated string use the below code

In JDK 8 or above use this

List<String> stringList = new ArrayList<String>(Arrays.asList("string1","string2","string3"));
String list= String.join(",", stringList
.stream()
.map(name -> ("'" + name + "'"))
.collect(Collectors.toList()));

System.out.println(list); // prints 'string1','string2','string3'

Convert an array of Ints to a comma separated string

Well you can do it like :

let formattedArray = ([0,1,1,0].map{String($0)}.joined(separator: ",")

How to concatenate array of integers into comma separated string

In your question, you've commented out the following snippet:

($arraylist-join -',')

because it returns the error Cannot convert value "," to type "System.Int32"...

The reason for this is the dash - in front of ','.

In PowerShell, only operators and parameters are prefixed with a dash, and since ',' is neither (it's an argument to an operator), the PowerShell parser gets super confused and tries to treat -',' as a value expression that would result in a negative number.

Just void the dash and you'll be fine:

$arraylist -join ','

Finally, you can easily cast an array of integers to an array of strings with the unchecked cast operator -as (PowerShell 3.0 and newer):

$StringArray = 1,2,3,4,5 -as [string[]]

or with an explicit cast (PowerShell 2.0-compatible):

$StringArray = [string[]]@(1,2,3,4,5)

Easy way to turn JavaScript array into comma-separated list?

The Array.prototype.join() method:

var arr = ["Zero", "One", "Two"];
document.write(arr.join(", "));


Related Topics



Leave a reply



Submit