Samual Sam has Published 2492 Articles

Left pad a String with a specified String in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:39:31

118 Views

The following is our string.String str = "Jack";Now take a StringBuilder object.StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length to 20. The string that will be padded comes on the left.while (strBuilder.length() + str.length() < 20) { strBuilder.append("demo"); }The following is an example wherein we have ... Read More

Display Month in MMMM format in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:37:32

9K+ Views

The MMMM format for months is like entire month name: January, February, March, etc. We will use it like this.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMMM format = "+strMonth);Above, we have used the SimpleDateFormat class, ... Read More

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

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:35:40

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 ... Read More

Java Program to check if the String contains only certain characters

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:33:25

988 Views

The following is our string.String str = "pqrst";In the above string, we want to search for the following set of characters.// set of characters to be searched char[] chSearch = {'p', 'q', r'};For this, loop through the length of the string and check every character in the string “str”. If ... Read More

Java Program to format date time with Join

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:31:35

256 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.Example Live Demopublic class Demo {    public static void main(String[] args) ... Read More

Right pad a string in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:30:47

778 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.Example Live Demopublic class Demo {    public static void ... Read More

Java Program to format a string

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:21:49

134 Views

To format a string, use the String.format() method in Java. The following is an example that formats a string %s.Example Live Demopublic class Demo {    public static void main(String []args) {       String str = String.format("%s %s", "demo", "text");       System.out.print("String: "+str);    } }OutputString: demo ... Read More

Perform Bubble Sort on strings in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:18:25

2K+ Views

To perform Bubble Sort, try the below given code. In this each each pair of adjacent elements is compared and the elements are swapped if they are not in order.The following is an example.Example Live Demopublic class Demo {    public static void main(String []args) {       String str[] ... Read More

Java Program to format strings into table

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:15:11

496 Views

To format strings into table, use the System.out.format.For our table, we have given 15s, that means 15 spaces. In the same way for the second column after the indices.|%1$-15s|%2$-15s|Added the above in a string, so that we don’t have add it again and again.The following is an example.Example Live Demopublic class ... Read More

Check whether the String contains only digit characters in Java

Samual Sam

Samual Sam

Updated on 27-Jun-2020 06:10:48

953 Views

The following is our string.String str = "4434";To check whether the above string has only digit characters, try the following if condition that uses matches() method and checks for every character.if(str.matches("[0-9]+") && str.length() > 2) { System.out.println("String contains only digits!"); }Example Live Demopublic class Demo {    public static void main(String ... Read More

Advertisements