Java Articles

Page 85 of 450

Format Calendar with String.format() in Java

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

Firstly, consider an object with date value.Object arrObj[] = { "Date", Calendar.getInstance() };After that, use the String.format() method to format Calendar and display the date.The following is an example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String []args){       Object arrObj[] = { "Date", Calendar.getInstance() };       System.out.println("Formatting Date...");       System.out.println(String.format("%1$s = %2$tY %2$tm %2$te", arrObj));    } }OutputFormatting Date... Date = 2018 11 17

Read More

Right pad a string in Java

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

To right pad a string, use the String.format and set the spaces.String.format("%1$-" + 20 + "s", "demotext"));If you add 30 above, it will display the next string after 30 spaces from the beginning.String.format("%1$-" + 30 + "s", "demotext")The following is an example.Examplepublic class Demo {    public static void main(String []args){       System.out.print(String.format("%1$-" + 20 + "s", "demotext"));       System.out.println("Right padded!");    } }Outputdemotext Right padded!

Read More

Left pad a string in Java

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 927 Views

To left pad a string, use the String.format and set the spaces.String.format("|%20s|", "demotext")If you add 30 above, it will display the first string after 30 spaces from the beginning.String.format("|%30s|", "demotext")Examplepublic class Demo {    public static void main(String []args) {       System.out.print(String.format("|%20s|", "demotext"));       System.out.println("Left padded!");    } }Output| demotext|Left padded

Read More

Line Separator in Java

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

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "one" + System.lineSeparator() + "two";       System.out.println(str);    } }Outputone twoLet us see another example. On Linux based system, the program will work correctly.Examplepublic class Demo {    public static void main(String[] args) {       String str = System.lineSeparator();       System.out.println((int) str.charAt(0));    } }Output10

Read More

Remove newline, space and tab characters from a string in Java

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

To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Examplepublic class Demo {    public static void main(String[] args) {       String originalStr = "Demo\tText";       System.out.println("Original String with tabs, spaces and newline: "+originalStr);       originalStr = originalStr.replaceAll("[\t ]", "");       System.out.println("String after removing tabs, spaces and new line: "+originalStr);    } }OutputOriginal String with tabs, spaces and newline: Demo Text String ...

Read More

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 382 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
Showing 841–850 of 4,496 articles
« Prev 1 83 84 85 86 87 450 Next »
Advertisements