0% found this document useful (0 votes)
59 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 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
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy