used trim() method ,used GetChar() method ,used setlength() method, used append() method
Previous | Home | Next |
example
//following Program using trim(), GetChar(), setlength(), append(). package r4r.co.in; public class NewClass { public static void main(String[] args) { StringBuffer s1 = new StringBuffer("Increament in StringBuffer .."); System.out.println("Output= " + s1 + "\nLength of String = " + s1.length() //String Concatenation + "\ncapacity of String = " + s1.capacity()); String s3 = "Increament in StringBuffer ".trim(); //whitespace is Trim System.out.print(s3 + "\nNew Length of String after Trime:" + s3.length()); System.out.print("\nValue at position 8 = " + s1.charAt(8)); //GetChar at position s1.setCharAt(8, 't'); System.out.println("\nNewChar at position 8 = " + s1.charAt(8)); //Char change at position s1.setLength(6); //GetChar length System.out.println("String value upto position 8 = " + s1); int s2 = 52; //capacity increase String s = s1.append("\nNew Capacity s2 = ").append(s2).append("!").toString(); System.out.println(s); } }
Output:
Output= Increament in StringBuffer .. Length of String = 30 capacity of String = 46 Increament in StringBuffer New Length of String after Trime:26 Value at position 8 = n NewChar at position 8 = t String value upto position 8 = Increa Increa New Capacity s2 = 52!
// Program to sorting an Array in Strings. package r4r.co.in; public class NewClass { static String arr[] = { "That", "Happen", "This", "Clock", "Ticking", "Evil", "Habbit", "Java", "Skype", "Bb", "Ca", "Too", "Gone", "Send", "Message", "Add", "Of", "Where", "country", "Addidas", "Nike", "Aa"}; public static void main(String[] args) { { //Shortion of an Array in Increasing order System.out.println("Array in Increasing order:"); for (int j = 0; j < arr.length; j++) { for (int i = j + 1; i < arr.length; i++) { if (arr[i].compareTo(arr[j]) < 0) { String t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } System.out.println(arr[j]); } } { //Shortion of an Array in Decreasing order System.out.println("\nArray in Decreasing order"); for (int j = arr.length - 1; j > 0; j--) { for (int i = j - 1; i < arr.length; i++) { if (arr[i].compareTo(arr[j]) < 0) { String t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } System.out.println(arr[j]); } } } }
Output:
Array in Increasing order: Aa Add Addidas Bb Ca Clock Evil Gone Habbit Happen Java Message Nike Of Send Skype That This Ticking Too Where country Array in Decreasing order Where Too Ticking This That Skype Send Of Nike Message Java Happen Habbit Gone Evil Clock Ca Bb Addidas Add Aa
Previous | Home | Next |