![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Sharon Christine](https://www.tutorialspoint.com/assets/profiles/13517/profile/60_82215-1512649681.jpg)
659 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
770 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!
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
202 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
489 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 Demo { public static void main(String []args) { String strFormat = "|%1$-15s|%2$-15s|"; System.out.format(strFormat, "One", "Two"); System.out.format(strFormat, "Three", "Four"); System.out.format(strFormat, "Five", "Six"); System.out.format(strFormat, "Seven", "Eight"); System.out.format(strFormat, "Nine", "Ten"); System.out.format(strFormat, ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
710 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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[] = { "s", "k", "r", "v", "n"}; String temp; System.out.println("Sorted string..."); for (int j = 0; j < str.length; j++) { for (int i = j + 1; i < str.length; i++) { ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
14K+ Views
To locate a substring in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a substring ‘demo’ in a string and get the index.int index = str.indexOf( 'demo');Example Live Demopublic class Demo { public static void main(String []args) { String str = "testdemo"; System.out.println("String: "+str); int index = str.indexOf("demo"); System.out.printf("Substring 'demo' is at index %d", index); } }OutputString: testdemo Substring 'demo' is at index 4
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
13K+ Views
To locate a character in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a character ‘d’ in a string and get the index.int index = str.indexOf( 'd');Example Live Demopublic class Demo { public static void main(String []args) { String str = "testdemo"; System.out.println("String: "+str); int index = str.indexOf( 'd' ); System.out.printf("'d' is at index %d, index); } }OutputString: testdemo 'd' is at index 4Let us see another example. The method returns -1, if the character isn’t found −Example Live Demopublic class ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
3K+ Views
Let’s say the following is our string with special characters.String str = "test*$demo";Check for the special characters.Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find();Now, if the bool value “val” is true, that would mean the special characters are in the string.if (val == true) System.out.println("Special characters are in the string.");Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String []args) { String str = "test*$demo"; System.out.println("String: "+str); Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
943 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 []args) { String str = "4434"; if(str.matches("[0-9]+") && str.length() > 2) { System.out.println("String contains only digits!"); } } }OutputString contains only digits!Let us see another example.Example Live Demopublic class Demo { public static void main(String []args) ... Read More