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 128 views
2 answers 110 views
3 answers 128 views
2 answers 93 views
1 answer 95 views
...