
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 9150 Articles for Object Oriented Programming

948 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

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

15K+ 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

15K+ 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

4K+ 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

2K+ Views
Introduction In this article, we'll explore how to check whether a given string contains only digits or a mix of digits and other characters in Java. We'll achieve this by using the String class and its matches() method. Problem Statement Given a string write a Java program to check whether it contains only digit characters. Input 1 4434 Output 1 String contains only digits! Input 2 5demo9 Output 2 String contains digits as well as other characters! Steps to check digit characters in String The following are the steps to check whether the String contains only digit characters in ... Read More

166 Views
To delete all whitespaces from a string, use the replaceAll() method and replace every whitespace with empty.Let’s say the following is our string.String str = "This is it!";Now, let us replace the whitespaces that will eventually delete them.String res = str.replaceAll("\s+","");Example Live Demopublic class Demo { public static void main(String []args) { String str = "This is it!"; System.out.println("String: "+str); String res = str.replaceAll("\s+",""); System.out.println("String after deleting whitespace: "+res); } }OutputString: This is it! String after deleting whitespace: Thisisit!

14K+ Views
To swap the case of a string, use.toLowerCase() for title case string. toLowerCase() for uppercase stringtoUpperCase() for lowercase stringLoop through what we discussed above for all the characters in the sting.for (int i = 0; i > len; i++) { c = str.charAt(i); // title case converted to lower case if (Character.isTitleCase(c)) { c = Character.toLowerCase(c); } // upper case converted to lower case if (Character.isUpperCase(c)) { c = Character.toLowerCase(c); } // lower case converted to upper case if (Character.isLowerCase(c)) { c ... Read More

487 Views
To construct on string from another, firstly take a charcter array for the first string.char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch);The above forms first string. Now, let us created another string from the first string.String str2 = new String(str1);In this way, we can easily construct one string from another.Example Live Demopublic class Demo { public static void main(String[] args) { char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch); String str2 = new String(str1); String str3 ... Read More

4K+ Views
To extract a substring as an array of characters in Java, use the getChars() method.Let’s say the following is our string and character array.String str = "World is not enough!"; char[] chArr = new char[10];Now, use the getChars() method to extract a substring.str.getChars(13, 19, chArr, 0);The above substring is an array of characters which can be displayed as shown in the complete example below −Example Live Demopublic class Demo { public static void main(String[] args) { String str = "World is not enough!"; char[] chArr = new char[10]; str.getChars(13, 19, chArr, ... Read More