How to Easily Remove the Last Comma from an Array

How can I easily remove the last comma from an array?

Alternatively you can use the rtrim function as:

$result = '';
foreach($array as $i=>$k) {
$result .= $i.'-'.$k.',';
}
$result = rtrim($result,',');
echo $result;

How to remove last comma and space in array? Java

Don't print it in the first place. Print the comma before the string and only when i > 0.

public static void printArray(int[] myArray)
{

System.out.print("[");
for(int i = 0; i < myArray.length; i++)
{
myArray[i] = i + 1;
if (i > 0)
{
System.out.print(", ");
}
System.out.print(myArray[i]);
}
System.out.println("]");
}

Removing the comma from the last element in the string of array

One way I like to handle this problem is to conditionally prepend a comma only for the second word onwards:

public static void printList(String[] words, int count) {
for (int i=0; i < count; i++) {
if (i > 0) System.out.print(",");
System.out.print(words[i]);
}
}

You could also build the output string you want, and then trim off the final comma:

StringBuilder sb = new StringBuilder();
for (int i=0; i < count; i++) {
sb.append(words[i]).append(",");
}
String output = sb.toString().replaceAll(",$", "");
System.out.println(output);

Remove last comma in for loop using array

To fix your code, I add a flag that indicates the first array element printed. For all subsequent array elements printed, I prepend the delimiter.

    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};
boolean first = true;
for (int i : array) {
if (i >= 50) {
if (first) {
first = false;
}
else {
System.out.print(", ");
}
System.out.print(i);
}
}
System.out.println();

Running the above code prints the following:

97, 123, 88, 200, 50, 99

Alternatively, you can use streams:

public static void main(String[] args) {
int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};
String result = Arrays.stream(array)
.filter(i -> i >= 50)
.boxed()
.map(i -> i.toString())
.collect(Collectors.joining(", "));
System.out.println(result);
  • Arrays.stream returns a stream of int
  • filter keeps only those elements in the array that are >= 50
  • boxed converts int (primitive) to Integer (object)
  • map converts Integer to String
  • Collectors.joining concatenates all elements in stream and separates each element with a comma followed by a space, i.e. ,

Running the above code prints the following:

97, 123, 88, 200, 50, 99

How can I remove the last comma from a map?

{mydata.map(({name}, idex) => (
<p style={{display:"inline"}}>
{" "}{name} {index === mydata.length - 1 ? "" : ","}
</p>
))}

Remove last comma in array

Well... you've been flamed to high heaven for no obvious reason, but the answer appears to be "you don't need to worry about that comma". images.length returns 3 and images.join(",") produces a string that doesn't include that trailing comma. So... even though it would appear that my initial answer of "this is bad syntax" is wrong, the reality is that that comma appears to do you no harm (unless I'm overlooking something). So... the question is why do you care if it's there or not if the interperter appears to be totally uninterested in it.

If there's some older Javascript environment that's not happy with this syntax then you should avoid using it.

If this javascript is being generated dynamically by some other process, then you should alter your question to specify what language/environment this is happening in, and I'm sure we'll help you squash that trailing comma.

Remove last comma (and possible whitespaces after the last comma) from the end of a string

This will remove the last comma and any whitespace after it:

str = str.replace(/,\s*$/, "");

It uses a regular expression:

  • The / mark the beginning and end of the regular expression

  • The , matches the comma

  • The \s means whitespace characters (space, tab, etc) and the * means 0 or more

  • The $ at the end signifies the end of the string



Related Topics



Leave a reply



Submit