Java Articles - Page 424 of 540

Java program to check if the string contains any character in the given set of characters

karthikeya Boyini
Updated on 15-Oct-2024 11:53:07

1K+ Views

In this article, we will learn to check if the string contains any character in the given set of characters in Java. We will iterate over the string and compare each character to a set of characters that we want to search for. If any match is found, the program will print the character and confirm that it is present in the string. Problem Statement Write a program in Java to check if the string contains any character in the given set of characters − Input str = abcde chSearch = 'b', 'c' Output Character b found in string abcde ... Read More

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

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 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

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

karthikeya Boyini
Updated on 27-Jun-2020 06:36:38

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

Left pad a String with a specified String in Java

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

237 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 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

Line Separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:40:15

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

Left pad a string in Java

Sharon Christine
Updated on 27-Jun-2020 06:22:59

921 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")Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.print(String.format("|%20s|", "demotext"));       System.out.println("Left padded!");    } }Output| demotext|Left padded

Right pad a string in Java

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

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.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!

Conversion characters for time in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:13:12

306 Views

The following are the conversion characters for date-time.CharacterDescriptioncComplete date and timeFISO 8601 dateDU.S. formatted date (month/day/year)T24-hour timer12-hour timeR24-hour time, no secondsYFour-digit year (with leading zeroes)yLast two digits of the year (with leading zeroes)CFirst two digits of the year (with leading zeroes)BFull month namebAbbreviated month namemTwo-digit month (with leading zeroes)dTwo-digit day (with leading zeroes)eTwo-digit day (without leading zeroes)AFull weekday nameaAbbreviated weekday namejThree-digit day of year (with leading zeroes)HTwo-digit hour (with leading zeroes), between 00 and 23kTwo-digit hour (without leading zeroes), between 0 and 23ITwo-digit hour (with leading zeroes), between 01 and 12lTwo-digit hour (without leading zeroes), between 1 and 12MTwo-digit minutes ... Read More

Java program to format strings into table

Samual Sam
Updated on 25-Oct-2024 11:25:30

1K+ Views

In this article, we will learn how to format strings into a table using Java. Java's System.out.format() method is a convenient way to display formatted output. We will define a string format that specifies column widths to ensure uniform spacing, allowing us to create a neat table layout. By using this approach, we can display multiple rows of string data in a structured and readable format without manually adjusting spaces for each entry. Steps to format strings into a table Following are the steps to format strings into a table − Define a format string ... Read More

Format Calendar with String.format() in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:16:23

995 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.Example Live Demoimport 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

Advertisements