How to remove the leading and trailing commas from a string in Java

1 Answer

0 votes
public class Main {
    public static void main(String[] args) {
        String str = ",,,Java,,";
        
        str = str.replaceAll("^,+|,+$", "");
        
        System.out.println(str);  
    }
}


 
/*
run:

Java
     
*/

 



answered Mar 5, 2025 by avibootz

Related questions

3 answers 158 views
2 answers 128 views
1 answer 107 views
3 answers 140 views
2 answers 109 views
1 answer 117 views
...