You can finish your task by combining/ concatenating your two stings using concat() method or + operator.Exampleimport java.util.Scanner; public class ConncatSample { public static void main(String []args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your first name"); String firstName = sc.nextLine(); System.out.println("Enter your last name"); String lastName = sc.nextLine(); String completeName = firstName+lastName; System.out.print("Hi your complete name is :: "); System.out.println(completeName); } }OutputEnter your first name KrishnaKasyap Enter your last name BhagavatulaHi your complete ... Read More
To import any package in the current class you need to use the import keyword asimport packagename;ExampleLive Demoimport java.lang.String;
public class Sample {
public static void main(String args[]) {
String s = new String("Hello");
System.out.println(s);
}
}OutputHello
The String class belongs to the java.lang package. This is the default package of the Java language therefore it is not mandatory to import it to use its classes.
Since String class is immutable once created we cannot modify the data of the string. But still if you want to manipulate string data you can rely on StringBuffer or StringBuilder classes.Examplepublic class Test {
public static void main(String args[]) {
String str = "Hi welcome ";
StringBuffer sb= new StringBuffer(str);
sb.append("to Tutorialspoint");
System.out.println(sb);
}
}OutputHi welcome to Tutorialspoint
Since String class is immutable once created we cannot modify the data of the string. But still, if you want to manipulate string data you can rely on StringBuffer or StringBuilder classes.Examplepublic class Test {
public static void main(String args[]) {
String str = "Hi welcome ";
StringBuffer sb= new StringBuffer(str);
sb.append("to Tutorialspoint");
System.out.println(sb);
}
}OutputHi welcome to Tutorialspoint
You can reverse the contents of a given String using leaving the spaces using the reverse() method of the StringBuffer class.Examplepublic class Test {
public static void main(String args[]) {
String str = "hi welcome to Tutorialspoint";
String strArray[] = str.split(" ");
StringBuffer sb= new StringBuffer(str);
sb.reverse();
for(int i=0 ; i
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.
To learn more about Strings visit Tutorialspoint Strings page.
The StringBuffer class contains a method known as deleteCharAt(). This method deletes the character at a specified index/position. You can use this method to delete/remove a particular character from a string in Java.Examplepublic class Test {
public static void main(String args[]){
String str = "hi welcome to Tutorialspoint";
StringBuffer sb= new StringBuffer(str);
sb.deleteCharAt(sb.length()-1);
System.out.println(sb);
}
}Outputhi welcome to Tutorialspoint