Remove last "." character/s from a string without removing in middle

D.madushanka :

I am reading a text file and create a excel file using it. I managed to do the conversion. But there is a description filed with one or two full-stop(.) that I wanna remove. Some times there may be have one full-stop, may be have two or may be no full-stop. I tried with using replace but it also remove the (.) inside/middle of the text. I don't wanna do that.

These are some samples

P/NO 9281195 . .                                 
TFR TO 202 . .                                                               
E/C 21118 GTLA/0267/0044505 NI                                                    
9259589 .PMGN JAYARATHNE .                       
GTLA/0267/0044505 662881724V .  

What I want is

P/NO 9281195                              
TFR TO 202                                                            
E/C 21118 GTLA/0267/0044505 NI                                                    
9259589 .PMGN JAYARATHNE                
GTLA/0267/0044505 662881724V
azro :
  1. Solution :

    You can use a regex that'll match some couple (space point) at the end, and replace this by nothing

    String line = "P/NO 9281195 . .";
    line = line.replaceAll("(\\s*\\.)+$", "");
    
  2. Details :

    • \s* for 0 to unlimited space
    • \. for a point
    • (\\s*\\.)+ for 1 to unlimited couples of space/point
    • $ match at the end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=112975&siteId=1