[java] Replacing last character in a String with java

I have a string:

String fieldName = "A=2,B=3 and C=3,";

Now I want to replace last , with space.

I have used:

if (fieldName.endsWith(",")) {
    fieldName.replace(",", " ");
    fieldName = fieldName.replace((char) (fieldName.length() - 1), 'r');
}

System.out.println("fieldName = " + fieldName);

But still I am getting the same old string. How I can get this output instead?

fieldName = A=2,B=3 and C=3

This question is related to java string

The answer is


Already @Abubakkar Rangara answered easy way to handle your problem

Alternative is :

String[] result = null;
if(fieldName.endsWith(",")) {                           
String[] result = fieldName.split(",");
    for(int i = 1; i < result.length - 1; i++) {
        result[0] = result[0].concat(result[i]);
    }
}

Firstly Strings are immutable in java, you have to assign the result of the replace to a variable.

fieldName = fieldName.replace("watever","");

You can use also use regex as an option using String#replaceAll(regex, str);

fieldName = fieldName.replaceAll(",$","");

i want to replace last ',' with space

if (fieldName.endsWith(",")) {
    fieldName = fieldName.substring(0, fieldName.length() - 1) + " ";
}

If you want to remove the trailing comma, simply get rid of the + " ".


You can simply use substring:

if(fieldName.endsWith(","))
{
  fieldName = fieldName.substring(0,fieldName.length() - 1);
}

Make sure to reassign your field after performing substring as Strings are immutable in java


To get the required result you can do following:

fieldName = fieldName.trim();
fieldName = fieldName.substring(0,fieldName.length() - 1);

Try this:

s = s.replaceAll("[,]$", "");

    if (fieldName.endsWith(",")) {
        fieldName = fieldName.substring(0, fieldName.length()-1) + " ";
    }

Modify the code as fieldName = fieldName.replace("," , " ");


StringBuilder replace method can be used to replace the last character.

StringBuilder.replace(startPosition, endPosition, newString)

StringBuilder builder = new StringBuilder(fieldName);
builder.replace(builder.length()-1, builder.length(), "");
builder.toString();

org.apache.commons.lang3.StringUtils.removeEnd() and org.springframework.util.StringUtils.trimTrailingCharacter() are your friends:

StringUtils.removeEnd(null, *)      = null
StringUtils.removeEnd("", *)        = ""
StringUtils.removeEnd(*, null)      = *
StringUtils.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
StringUtils.removeEnd("www.domain.com", ".com")   = "www.domain"
StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeEnd("abc", "")    = "abc"
    @Test
    public void springStringUtils() {
        String url = "https://some.site/path/";

        String result = org.springframework.util.StringUtils.trimTrailingCharacter(url, '/');

        assertThat(result, equalTo("https://some.site/path"));
    }

you can use regular expressions to identify the last comma (,) and replace it with " " as follow:

if(fieldName.endsWith(","))
{                           
fieldName = fieldName.replace(/,([^,]*)$/," ");
}

fieldName = fieldName.substring(0, string.length()-1) + " ";

You can simply use :

if(fieldName.endsWith(","))
{
   StringUtils.chop(fieldName);
}

from commons-lang