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.Example Live Demopublic 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.Example Live Demopublic class Demo { public static void main(String[] args) { String str = System.lineSeparator(); System.out.println((int) str.charAt(0)); } }Output10
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 left padded a string with another string “demo”Example Live Demopublic class Demo { public static void main(String[] args) { String str = "Jack"; StringBuilder strBuilder = new StringBuilder(); // left padding with a string while (strBuilder.length() + str.length() ... Read More
The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc. Here, we will use the following.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMM format = "+strMonth);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); ... Read More
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, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); ... Read More
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.Example Live Demopublic 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 ... Read More
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.Example Live Demopublic 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 ... Read More
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.Example Live Demopublic class Demo { public static void main(String[] args) { String str = String.join("$","Demo","Text"); System.out.println("Joined strings: "+str); } }OutputJoined strings: Demo$Text
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) { 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
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 main(String []args){ System.out.print(String.format("%1$-" + 20 + "s", "demotext")); System.out.println("Right padded!"); } }Outputdemotext Right padded!
A block chain is a shared, distributed ledger which consists of a structure of data where all the financial ledger entries are digitally signed to ensure their authenticity. This enables higher security for the transactions and no one can tamper it. The block chain technology is showing higher promise for the businesses due to its data integration strategy.Block chain technology supports Bitcoin and other cryptocurrencies too.