Skip to main content

String Builder | Reverse a String (using StringBuilder class)| Get A Character from Index| Lecture 13 |

 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);

   }

}

 




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());

   }

}

 



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.


Comments

Popular posts from this blog

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

Recursion in One Shot | Theory + Question Practice + Code | Level 1 - Easy

JAVA Recursion Class 1 (Codes) Q1. Print numbers from 5 to 1. public static void printNumbers(int n) { if(n == 0) { return; } System.out.println(n); printNumbers(n-1); } Q2. Print numbers from 1 to 5. public static void printNumbers(int n) { if(n == 6) { return; } System.out.println(n); printNumbers(n+1); } Q3. Print the sum of first n natural numbers. class Recursion1 { public static void printSum(int n, int sum) { if(n == 0) { System.out.println(sum); return; } sum += n; printSum(n-1, sum); } public static void main(String args[]) { printSum(5, 0); } } Q4. Print factorial of a number n. class Recursion1 { public static void printFactorial(int n, int fact) { if(n == 0) { System.out.println(fact); return; } fact *= n; printFactorial(n-1, fact); } public static void main(String args[]) { printFactorial(5, 1); } } Q5. Print the fibonacci sequence till nth term. class Recursion1 { public static void printFactorial(int a, int b, int n) { if(n == 0) { return; } System.out.println(a); printF...