1. Write a program to concat two string
Method name: String.concat();
public class Main { public static void main(String[] args) { String sentence1="Hello "; System.out.println(sentence1.concat("Java")); } }
Output:
Hello Java
Method name: String.concat();
public class Main { public static void main(String[] args) { String sentence1="Hello "; System.out.println(sentence1.concat("Java")); } }
Output:
Hello Java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers={14,12,9,15,17,21,23,5,8,25};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
Output:
[5, 8, 9, 12, 14, 15, 17, 21, 23, 25]
Java HashMap is a map interface that allows us to store data by key-value pair where key should be unique.
HashMap Syntax:
HashMap<datatype1, datatype2obj= new HashMap<datatype1, datatype2();
Let’s see a simple example of HashMap to store key and … Read more
File handling is very important topic in Java. We need to perform various tasks like create file, read and write file, modify file, delete file, etc. We will cover below topics in this article.
… Read moreMethod to remove extra white space:
String.trim();
A simple Java program:
public class Main {
public static void main(String[] args) {
String s = " Hello Java "; //adding white space before and after string
System.out.println(s.trim());
}
}
Output:
Hello
… Read more Method for convert to lower case:
String.toLowerCase();
Method for convert to UPPER case:
String.toUpperCase();
A simple Java program:
public class Main {
public static void main(String[] args) {
String s="Hello Java";
System.out.println(s.toLowerCase()); //Convert to lower case
System.out.println(s.toUpperCase()); //Convert to upper
… Read more You can get part of a string by following two methods:
String replace method is: String.replace(“oldValue”,”newValue”);
public class Main {
public static void main(String[] args) {
String sentence1="Java programming is very hard";
String newSentence=sentence1.replace("hard","easy");
System.out.println(newSentence);
}
}
Output:
Java programming is very easy
… Read more Method to check if a string is empty or not: String.isEmpty();
public class Main {
public static void main(String[] args) {
String string1="Hello Java";
String string2="";
boolean str1=string1.isEmpty(); //expected: should be false
boolean str2=string2.isEmpty(); //expected: should be true
System.out.println(str1);
System.out.println(str2);
… Read more
public class Main {
public static void main(String[] args) {
String format = "dd-MM-yyyy hh:mm:ss aa";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); //passing format as constructor
String date = simpleDateFormat.format(new Date());
System.out.println(date);
}
}
Output:
12-12-2020 04:06:07 PM
… Read more