How to Remove last Comma?

Asked
Active3 hr before
Viewed126 times

9 Answers

commaremove
90%

We remove the last comma of a string by using the built-in substring() method with first argument 0 and second argument string.length()-1 in Java.,Slicing starts from index 0 and ends before last index that is string.length()-1.,In this tutorial, we are going to learn about how to remove the last comma (,) of a given string in Java.,In this demo, we are going to learn about how to rotate an image continuously using the css animations.

String str = "Hello,";

// remove last character (,)
str = str.substring(0, str.length() - 1);

System.out.println(str);
load more v
88%

It uses a regular expression:,This will remove the last comma and any whitespace after it:,The / mark the beginning and end of the regular expression,First, one should check if the last character is a comma. If it exists, remove it.

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

str = str.replace(/,\s*$/, "");
load more v
72%

Yes, you can remove last comma using Regex with .replace() function in Javascript,I have a string like "this, is, test," now I want to remove the last "," (comma) from this string to make it like "this, is, test", so How can I remove only last comma from string jQuery or Javascript?,If you want to remove double comma from a string and make it one, you can use regex,Above method works as needed, but if you don't want to use Regex you can try the below Javascript based function:

Here is the example of it

var str1 = "This, is, test,";

str1 = str1.replace(/,\s*$/, ""); // output "this, is , test"

Here is the Fiddle working sample  https://jsfiddle.net/xkgy71dr/

function removeExtraComma() {
   var str1 = "This, is, test,";

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

   document.getElementById("Output").innerHTML = str1;
}

removeExtraComma();

HTML

<div id="Output">

</div>

If you want to remove double comma from a string and make it one, you can use regex

     var str1 = "this,,is,test";

     //remove extra comma from the middle of the string (if any)
     str1 = str1.replace(/^,|,$|(,)+/g, '$1');

     //output : this,is,test
load more v
65%

const text = 'abcdef'
const editedText = text.slice(0, -1) //'abcde'
load more v
75%

In java, the replaceAll() method can be used to replace all the occurrence of a single character, or a substring from a given string. The replaceAll() method belongs to the java.lang.String class.,The replaceAll() method takes two parameters.,, How to replace String Characters In Java Using replace() Method?,Updated: The character/substring used to replace the initial string.

Syntax:

public String replaceAll(String initial, String updated);
load more v
40%

The , matches the comma,The / mark the beginning and end of the regular expression,The s means whitespace characters (space, tab, etc) and the * means 0 or more,It uses a regular expression:

var str = 'This, is a test.';
alert(removeLastComma(str)); // should remain unchanged

var str = 'This, is a test,';
alert(removeLastComma(str)); // should remove the last comma

var str = 'This is a test,          ';
alert(removeLastComma(str)); // should remove the last comma

function removeLastComma(strng) {
   var n = strng.lastIndexOf(",");
   var a = strng.substring(0, n)
   return a;
}
load more v
22%

/ specifies the beginning and end of the regular expression.,Javascript remove last comma from a string using replace() and RegExp,Javascript remove last comma from a string using slice() ,Javascript remove last comma from a string using substring() and indexOf()

Code:-

const dummyString = "This is a dummy string, for example purposes,"
let stringWithoutComma = dummyString.replace(/,*$/, "")
console.log("Original String: " + dummyString)
console.log("Final String: " + stringWithoutComma)
load more v
60%

One may use string utility methods such as StringUtil.join to concatenate elements in an array or a collection object. Consult the StringUtil API's StringUtil.join entry.,Connect and share knowledge within a single location that is structured and easy to search., Linear constraint for dynamic facility location , Podcast 378: The paranoid style in application development

For example:

StringUtils.join(["a", "b", "c"], "--") // => "a--b--c"
load more v
48%

Remove last comma using jQuery ,You can remove any last specific character if it exists at last position with the help of given example.,What if there are multiple commas at the end of the string.for ex. '1,2,3,4,,,, ,,', Remove duplicate values from array in jQuery

<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnCheck').click(function (event) {
        var ids = $('.txtValue').val();
        var lastChar = ids.slice(-1);
        if (lastChar == ',') {
          ids = ids.slice(0, -1);
        }
        alert(ids);
      });
    });
  </script>
</head>
<body>
   <div>
    <input type="text" class="txtValue" value="1,2,3,4," /><br /><br />
    <button class="btnCheck">Check</button>
  </div>
</body>
</html>

Other "comma-remove" queries related to "How to Remove last Comma?"