Java Articles

Page 89 of 450

Check if a String is whitespace, empty ("") or null in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Let’s say the following is our string.String myStr = "";Now, we will check whether the above string is whitespace, empty ("") or null.if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {    System.out.println("String is not null or not empty or not whitespace"); } else {    System.out.println("String is null or empty or whitespace"); }The following is an example that checks for an empty string.Examplepublic class Demo {    public static void main(String[] args) {       String myStr = "";       if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {          System.out.println("String is not null or ...

Read More

Join Strings in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as shown below −String.join("$","Demo","Text");The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = String.join("$","Demo","Text");       System.out.println("Joined strings: "+str);    } }OutputJoined strings: Demo$Text

Read More

Java Program to format date time with Join

Samual Sam
Samual Sam
Updated on 11-Mar-2026 388 Views

To format date time with Join, set the date as a string and do not forget to add the delimeter.For delimeter “/” in the dateString.join("/","11","11","2018");For delimeter “:” in the date.String.join(":", "10","20","20");The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String d = String.join("/","11","11","2018");       System.out.print("Date: "+d);       String t = String.join(":", "10","20","20");       System.out.println("Time: "+t);    } }OutputDate: 11/11/2018 Time: 10:20:20

Read More

Get the substring after the first occurrence of a separator in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 9K+ Views

We have the following string with a separator.String str = "Tom-Hanks";We want the substring after the first occurrence of the separator i.e.HanksFor that, first you need to get the index of the separator and then using the substring() method get, the substring after the separator.String separator ="-"; int sepPos = str.indexOf(separator); System.out.println("Substring after separator = "+str.substring(sepPos + separator.length()));The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Tom-Hanks";       String separator ="-";       int sepPos = str.indexOf(separator);       if (sepPos == -1) { ...

Read More

Get the substring before the last occurrence of a separator in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 6K+ Views

We have the following string with a separator.String str = "David-Warner";We want the substring before the last occurrence of a separator. Use the lastIndexOf() method.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.lastIndexOf(separator); System.out.println("Substring before last separator = "+str.substring(0, sepPos));The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "David-Warner";       String separator ="-";       int sepPos = str.lastIndexOf(separator);       if (sepPos == -1) {          System.out.println("");       ...

Read More

Count how many times the substring appears in the larger String in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 400 Views

Let’s say we have the following string.String str = "Learning never ends! Learning never stops!";In the above string, we need to find out how many times the substring “Learning” appears.For this, loop until the index is not equal to 1 and calculate.while ((index = str.indexOf(subString, index)) != -1) { subStrCount++; index = index + subString.length(); }The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Learning never ends! Learning never stops!";       System.out.println("String: "+str);       int subStrCount = 0;       String subString = ...

Read More

Java Program to display double and single quote in a string

Samual Sam
Samual Sam
Updated on 11-Mar-2026 6K+ Views

The following are our strings with single and double quote.String str1 = "This is Jack's mobile"; String str2 = ""This is it"!";Above, for single quote, we have to mention it normally like.This is Jack's mobileHowever, for double quotes, use the following and add a slash in the beginning as well as at the end.String str2 = ""This is it"!";The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str1 = "This is Jack's mobile";       String str2 = ""This is it"!";       System.out.println("Displaying Single Quote: "+str1);   ...

Read More

Java Program to split a string with dot

Samual Sam
Samual Sam
Updated on 11-Mar-2026 682 Views

Let’s say the following is our string.String str = "Java is a programming language. James Gosling developed it.";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split("\.");Above, we have split the string with dot as you can see under the split methods parameter.The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Java is a programming language. James Gosling developed it.";       System.out.println("String: "+str);       String[] strSplit = str.split("\.");       System.out.println("Splitting ...

Read More

Convert the Current Time to a java.sql.Date Object

Samual Sam
Samual Sam
Updated on 11-Mar-2026 712 Views

Firstly, create a Calendar class object.Calendar calendar = Calendar.getInstance();Now, import the following package.import java.sql.Date;Using a Date class now and creating an object would belong to the above package. Convert the current time to the java.sql.Date Object.Date sqlDate = new Date((calendar.getTime()).getTime());The following is an example.Exampleimport java.util.Calendar; import java.sql.Date; import java.text.ParseException; public class Demo {    public static void main(String[] args) throws ParseException {       Calendar calendar = Calendar.getInstance();       // object       Date sqlDate = new Date((calendar.getTime()).getTime());       System.out.println(sqlDate);    } }Output2018-11-19

Read More

Java Program to convert Date into milliseconds

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

Import the following package to work with Date class.import java.util.Date;No create a Date object.Date d = new Date();Let us convert the current date to milliseconds.d.getTime()The following is an example.Exampleimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.println("Date = " + d);       System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT = " + d.getTime());    } }OutputDate = Mon Nov 19 06:30:11 UTC 2018 Milliseconds since January 1, 1970, 00:00:00 GMT = 1542609011369

Read More
Showing 881–890 of 4,498 articles
« Prev 1 87 88 89 90 91 450 Next »
Advertisements