Tuesday, 8 March 2016

Example on StringBuilder in java


EXAMPLE 1 : Given a String "Maj1e4s57ti3c" You have to remove only digits from the given String to form a new String without any digit.

public class StrBuildr {
       public static void main(String args[]){
              String s = "Maj1e4s57ti3c";
              char[] cs = s.toCharArray();
             
              StringBuilder sb = new StringBuilder();
             
              for (char c : cs) {
                     if(!Character.isDigit(c)){
                           sb = sb.append(c);
                     }
              }
              String finalStr = sb.toString();
              System.out.println("After removing digits : "+ finalStr);
       }
}   

OUTPUT :
After removing digits : Majestic  




You may also like to know about :

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.