|
| 1 | +/* Manipulation and methods of String Buffer, removing white space, decimal formatting */ |
| 2 | +import java.util.Random; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.text.DecimalFormat; |
| 5 | + |
| 6 | +class Stringbuf { |
| 7 | + public static void main(String[] args) { |
| 8 | + // constructor 1 |
| 9 | + StringBuffer sb = new StringBuffer(); |
| 10 | + System.out.println(sb.capacity()); |
| 11 | + |
| 12 | + // constructor 2 |
| 13 | + StringBuffer sb2 = new StringBuffer("Kunal Kushwaha"); |
| 14 | + |
| 15 | + // constructor 3 |
| 16 | + StringBuffer sb3 = new StringBuffer(30); |
| 17 | + System.out.println(sb3.capacity()); |
| 18 | + |
| 19 | + |
| 20 | + sb.append("WeMakeDevs"); |
| 21 | + sb.append(" is nice!"); |
| 22 | + |
| 23 | + // sb.insert(2, " Rahul "); |
| 24 | + |
| 25 | + sb.replace(1, 5, "Kushwaha"); |
| 26 | + |
| 27 | + sb.delete(1, 5); |
| 28 | + |
| 29 | + // sb.reverse(); |
| 30 | + |
| 31 | + String str = sb.toString(); |
| 32 | + System.out.println(str); |
| 33 | + |
| 34 | + int n = 20; |
| 35 | + String name = RandomString.generate(n); |
| 36 | + System.out.println(name); |
| 37 | + |
| 38 | + // remove whitespaces |
| 39 | + |
| 40 | + String sentence = "Hi h hjh hjkso siowi w "; |
| 41 | + System.out.println(sentence); |
| 42 | + |
| 43 | + System.out.println(sentence.replaceAll("\\s", "")); |
| 44 | + |
| 45 | + // split |
| 46 | + |
| 47 | + String arr = "Kunal,Apoorv,Rahul,Snehal"; |
| 48 | + String[] names = arr.split(","); |
| 49 | + System.out.println(Arrays.toString(names)); |
| 50 | + |
| 51 | + // rounding off |
| 52 | + DecimalFormat df = new DecimalFormat("00.0000"); |
| 53 | + System.out.println(df.format(7.29)); |
| 54 | + } |
| 55 | +} |
0 commit comments