You are on page 1of 26

StringBuffer

• To create mutable strings


• Similar to String class except it is mutable
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 StringBuffer s = new StringBuffer("");
6 System.out.println(s.capacity());
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 StringBuffer s = new StringBuffer("Pokemon");
6 System.out.println(s);
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 String s = "Today";
6 StringBuffer sb = new StringBuffer(s.length());
7 System.out.println(sb.capacity());
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 String s = "Today";
6 StringBuffer sb = new StringBuffer(s.length());
7 System.out.println(s.capacity());
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBufferDemo {
4 public static void main(String[] args) {
5 StringBuffer buff = new StringBuffer("Internet of things");
6 System.out.println(buff.capacity());
7 buff = new StringBuffer(" ");
8 System.out.println(buff.capacity());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBufferExample{
4 public static void main(String args[]){
5 StringBuffer sb = new StringBuffer("Good ");
6 sb.append("Job");
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBufferSetCharAtExample1 {
4 public static void main(String[] args) {
5 StringBuffer sb = new StringBuffer("Jeva");
6 System.out.println(sb);
7 System.out.println(sb.charAt(1));
8 sb.setCharAt(1, 'a');
9 System.out.println(sb);
10 System.out.println(sb.charAt(1));
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBufferSetCharAtExample2 {
4 public static void main(String[] args) {
5 StringBuffer sb = new StringBuffer("Try");
6 System.out.println(sb);
7 sb.setCharAt(3,'x');
8 System.out.println(sb);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 import java.util.Scanner;
4 public class StringBufferSetCharAtExample3 {
5 public static void main(String[] args) {
6 StringBuffer sb = new StringBuffer("");
7 Scanner sc = new Scanner(System.in);
8 sb.append(sc.nextLine());
9 int index = sc.nextInt();
10 char ch = sc.next().charAt(0);
11 sb.setCharAt(index, ch);
12 System.out.println(sb);
13 }
14 }
15
16
17
18
19
20
21
22
Question 1
Write a Java code to toggle the case in the given string

Sample Input: Sample Output:


I am Sam i AM sAM
1 import java.util.Scanner;
2 public class Main{
3 public static void main(String args[]) {
4 Scanner scan = new Scanner(System.in);
5 String str = scan.nextLine();
6 StringBuffer sb = new StringBuffer(str);
7 int str_len = sb.length();
8 for(int i = 0; i < str_len; i++){
9 char ch = sb.charAt(i);
10 if(ch >= 'a' && ch <= 'z'){
11 int offset = ch - 'a';
12 ch = (char) ('A' + offset);
13 }
14 else if(ch >= 'A' && ch <= 'Z'){
15 int offset = ch - 'A';
16 ch = (char) ('a' + offset);
17 }
18 sb.setCharAt(i, ch);
19 }
20 System.out.println(sb);
21 }
22 }
StringBuilder
• To create mutable strings
• Similar to StringBuffer
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 StringBuilder s = new StringBuilder("");
6 System.out.println(s.capacity());
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 StringBuilder s = new StringBuilder("Hard work");
6 System.out.println(s);
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String []args) {
5 String s = "Tomorrow";
6 StringBuffer sb = new StringBuffer(s.length());
7 System.out.println(sb.capacity());
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBuilderExample{
4 public static void main(String args[]){
5 StringBuilder sb=new StringBuilder("Smart");
6 sb.append(" Work");
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBuilderExample6{
4 public static void main(String args[]){
5 StringBuilder sb = new StringBuilder();
6 System.out.println(sb.capacity());
7 sb.append("Hive");
8 System.out.println(sb.capacity());
9 sb.append("Java is my favourite language");
10 System.out.println(sb.capacity());
11 }
12 }
13
14
15
16
17
18
19
20
21
22
StringTokenizer
• To break string into tokens
• The set of delimiters (the characters that separate tokens) may be specified either
at the creation time or on a per-token basis.
• Package: java.util.StringTokenizer
1 // Predict the output
2
3 import java.util.StringTokenizer;
4 public class Simple{
5 public static void main(String args[]){
6 StringTokenizer st = new StringTokenizer("Lets try this");
7 System.out.println(st.nextToken());
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 import java.util.StringTokenizer;
4 public class Simple{
5 public static void main(String args[]){
6 StringTokenizer st = new StringTokenizer("Lets try this");
7 while(st.hasMoreTokens()) {
8 System.out.println(st.nextToken());
9 }
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 import java.util.StringTokenizer;
4 public class Simple{
5 public static void main(String args[]){
6 StringTokenizer st = new StringTokenizer("Let,s,try,this",",");
7 while (st.hasMoreTokens()) {
8 System.out.println(st.nextToken());
9 }
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 import java.util.StringTokenizer;
4 public class Main
5 {
6 public static void main(String args[])
7 {
8 StringTokenizer st3 =
9 new StringTokenizer("JAVA:Code:String", ":", true);
10 while (st3.hasMoreTokens())
11 System.out.println(st3.nextToken());
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 import java.util.StringTokenizer;
4 public class Main
5 {
6 public static void main(String args[])
7 {
8 StringTokenizer st3 =
9 new StringTokenizer("JAVA:Code:String", ":", false);
10 while (st3.hasMoreTokens())
11 System.out.println(st3.nextToken());
12 }
13 }
14
15
16
17
18
19
20
21
22
THANK YOU

You might also like