String , String Builder, StringBuffer in Java

 How to declare string

Strings are immutable.
String name = "Tony";
import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println(name);
    }
}
length() it return length of the given string.
charAt() it return a char value at the given index number
concat() it helps to join two string
compareTo() it helps to compare two string it check s1==s2, s1<s2, s1>s2
substring() it give the begining index and ending index. it find exact string or word.
Integer.parseInt() it convert String to number.
Integer.toString() it convert Number to String.
.getClass().getSimpleName() it check the dataType.

Q.1 Take an array of String input from user and find comulative length of all those string.
import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
     System.out.print("enter Size of Array: ");
     Scanner sc = new Scanner(System.in);
     int a = sc.nextInt();
     int myStrLength = 0;
     String myArray[] = new String[a];
     
     for(int i = 0; i<a; i++){
         myArray[i] = sc.nextLine();
    }
   
    for(int i = 0; i<myArray.length; i++){
        myStrLength += myArray[i].length();
    }
   
    System.out.print("Length is: "+ myStrLength);
    }
}
Q. 2 Input a string from user. Create a new String called result in which you  you will replace the letter ‘e’ in the original string with letter ‘i’.
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
        System.out.print("Enter Value: ");
        String str = sc.nextLine();
        System.out.println("Original ="+str);
        int size = str.length();
        String result = "";
        // System.out.print("result = "+ result.charAt(2));
        for(int i = 0; i<size; i++){
            if(str.charAt(i) == 'e'){
                result += 'i';
            }else{
                result += str.charAt(i);
            }
        }
        System.out.println("result = " +result);
    }
}
Q.3 Input an email from the user. You have to create a username from the email by deleting the part that comes after ‘@’. Display that username to the user.
import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String email = sc.nextLine();
        String userName = "";
        for(int i = 0; i<email.length(); i++){
            if(email.charAt(i) == '@'){
                break;
            }else{
                userName += email.charAt(i);
            }
        }
        System.out.println("Username is :"+userName);
    }
}
String Builder
StringBuilder in Java is a class used to create a mutable, or in other words, a modifiable succession of characters.
If we create a String variable it store in stack and the value store in Heap. for show in fig. str variable point to their value.
but if we modify, means string is immutable ( means do not change) so if we change the string it create new string and then change. it will consume your memory. 
this problem solved using String builder. string builder helps to directly update the existing string. it not consume any space. 

How to declare string builder
 StringBuilder sb = new StringBuilder("Tony");
setCharAt() set Charctor at index sb.setCharAt(0, 'p');
insert() insert charctor at index sb.insert(2, 'p');
delete() delete charctor at index sb.delete(start, end);
append() attach in last sb.append("_stark");

Reverse String problem using stringbuilder
class HelloWorld {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("HELLO");

        String temp ="a";
        for(int i = 0;i<sb.length()/2; i++){
            int front = i;
            int back = sb.length()-1-i;
           
           char frontChar = sb.charAt(front);
           char backChar = sb.charAt(back);
           
           sb.setCharAt(front, backChar);
           sb.setCharAt(back, frontChar);
        }
        System.out.println(sb);
    }
}
OLLEH
String Buffer
StringBuffer can creates an object and stores value in it which can be changed/ altered at any point of time i.e. String Buffer is mutable.
StringBuffer sb = new StringBuffer("shreyash");
The difference between StringBuilder and StringBuffer.
  1. StringBuffer is synchronized and StringBuilder is non-synchronized
  2. StringBuffer is less efficient and StringBuilder is more efficient.
  3. StringBuffer is faster than StringBuilder. ( StringBuilder take more time )