0

I have the following string and I want to remove dynamic number of dot(.) at the end of the String.

"abc....."

Dot(.) can be more than one

T8Z
  • 691
  • 7
  • 17

3 Answers3

4

Try this. It uses a regular expression to replace all dots at the end of your string with empty strings.

yourString.replaceAll("\\.+$", "");
Riaan Nel
  • 2,425
  • 11
  • 18
3

Could do this to remove all .:

String a = "abc.....";
String new = a.replaceAll("[.]", "");

Remove just the trailing .'s:

String new = a.replaceAll("//.+$","");

Edit: Seeing the comment. To remove last n .'s

int dotsToRemove = 5; // whatever value n
String new = a.substring(0, s.length()-dotsToRemove);
anon
  • 1,258
  • 10
  • 17
0

how about using this function? seems to work faster than regex

public static String trimPoints(String txt)
    {
        char[] cs = txt.toCharArray();
        int index =0;
        for(int x =cs.length-1;x>=0;x--)
        {
            if(cs[x]=='.')
                continue;
            else
                {
                index = x+1;
                break;
                }
        }

        return txt.substring(0,index);
    }
dsncode
  • 2,407
  • 2
  • 22
  • 36