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 ofint
filter
keeps only those elements in the array that are >= 50boxed
convertsint
(primitive) toInteger
(object)map
convertsInteger
toString
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 expressionThe
,
matches the commaThe
\s
means whitespace characters (space, tab, etc) and the*
means 0 or moreThe
$
at the end signifies the end of the string
Related Topics
How to Make MySQLi Connect Function
Accessing Array Values Using Array Key from Twig
Windows 7 PHP + Symfony2 Terribly Slow
Cannot Pass Null Argument When Using Type Hinting
Php: How to Get a List of Classes That Implement Certain Interface
PHP Exec() Is Not Executing the Command
How to Always Use Ignore-Platform-Reqs Flag When Running Composer
Best Practice for Working with Currency Values in PHP
This Is the .Htaccess Code in Wordpress. Can Someone Explain How It Works
Htaccess Access to File by Ip Range
Parse Timezone Int to String (Timezone Name) (Facebook API)
Reading Ssl Page with Curl (Php)
Have Trouble Installing PHPmyadmin on PHP7 Apache/2.4.7 (Ubuntu)