0% found this document useful (0 votes)
50 views

Java - Introduction To Programming: String Builder

The document discusses various methods of the StringBuilder class in Java, including how to: - Declare and initialize a StringBuilder - Get/set characters at specific indices - Insert/delete characters at indices - Append characters to the end - Get the length of a StringBuilder - Reverse a StringBuilder using a for loop to swap characters It provides code examples for each method and encourages practicing these StringBuilder methods as homework problems.

Uploaded by

Anda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Java - Introduction To Programming: String Builder

The document discusses various methods of the StringBuilder class in Java, including how to: - Declare and initialize a StringBuilder - Get/set characters at specific indices - Insert/delete characters at indices - Append characters to the end - Get the length of a StringBuilder - Reverse a StringBuilder using a for loop to swap characters It provides code examples for each method and encourages practicing these StringBuilder methods as homework problems.

Uploaded by

Anda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java - Introduction to Programming

Lecture 13

String Builder

Declaration
StringBuilder sb = new StringBuilder("Apna College");
System.out.println(sb);

Get A Character from Index


StringBuilder sb = new StringBuilder("Tony");
//Set Char
System.out.println(sb.charAt(0));

Set a Character at Index


StringBuilder sb = new StringBuilder("Tony");
//Get Char
sb.setCharAt(0, 'P');
System.out.println(sb);

Insert a Character at Some Index


import java.util.*;

public class Strings {


public static void main(String args[]) {
StringBuilder sb = new StringBuilder("tony");
//Insert char
sb.insert(0, 'S');
System.out.println(sb);
}
}

Apna College
Delete char at some Index
import java.util.*;

public class Strings {


public static void main(String args[]) {
StringBuilder sb = new StringBuilder("tony");
//Insert char
sb.insert(0, 'S');
System.out.println(sb);

//delete char
sb.delete(0, 1);
System.out.println(sb);
}
}

Append a char
Append means to add something at the end.
import java.util.*;

public class Strings {


public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Tony");
sb.append(" Stark");
System.out.println(sb);
}
}

Print Length of String


import java.util.*;

public class Strings {


public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Tony");
sb.append(" Stark");
System.out.println(sb);

System.out.println(sb.length());
}
}

Apna College
Reverse a String (using StringBuilder class)

import java.util.*;

public class Strings {


public static void main(String args[]) {
StringBuilder sb = new StringBuilder("HelloWorld");

for(int i=0; i<sb.length()/2; i++) {


int front = i;
int back = sb.length() - i - 1;

char frontChar = sb.charAt(front);


char backChar = sb.charAt(back);

sb.setCharAt(front, backChar);
sb.setCharAt(back, frontChar);
}

System.out.println(sb);
}
}

Homework Problems
Try Solving all the String questions with StringBuilder.

Apna College

You might also like