Great Deals

StringBuilder Demo

14:35 suresh chaudhari 0 Comments




public class StringBuilderDemo {

     public static void main(String[] args) {
            StringBuilder sb = new StringBuilder("Shiv");
          
               System.out.println("Original String " + sb);
            
               System.out.println("substring: " + sb.substring(0,2));
          
 //  StringBuilder java.lang.StringBuilder.replace(int start, int end, String str)
               sb = sb.replace(4,4,"!!!!");
               System.out.println("replace String from location 4 to 4 charecters from location 4  " + sb);
            
               System.out.println("indexOf() charecter S is   " + sb.indexOf("S"));
            
    //Syntax StringBuilder java.lang.StringBuilder.insert(int offset, String str)
               sb = sb.insert(5, "is");
               System.out.println("inserted String  " + sb);
              
              //Syntax StringBuilder java.lang.StringBuilder.append(String str)
               sb = sb.append(" lord");
               System.out.println("append: " + sb);
          
               System.out.println("lastIndexOf charecter i is  " + sb.lastIndexOf("i"));
               sb = sb.reverse();
               System.out.println("reverse: " + sb);
     }

}


0 comments:

Advertising