- Trending Categories
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 9321 Articles for Object Oriented Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
946 Views
A substring is a part of a string and it can be checked if a particular substring is present in the given string or not. An example of this is given as follows −String = The sunset is beautiful Substring = sunsetThis substring is present in the string.A program that demonstrates this is given as follows −Example Live Demopublic class Example { public static void main(String args[]) { String str = "Apples are red"; String substr = "are"; int n1 = str.length(); int n2 = substr.length(); System.out.println("String: " + str); System.out.println("Substring: " + substr); for (int i = 0; i
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
234 Views
To check whether the entered value is ASCII 7-bit printable, check whether the characters ASCII value is greater than equal to 32 and less than 127 or not. These are the control characters.Here, we have a character.char one = '^';Now, we have checked a condition with if-else for printable characters.if (c >= 32 && c < 127) { System.out.println("Given value is printable!"); } else { System.out.println("Given value is not printable!"); }Example Live Demopublic class Demo { public static void main(String []args) { char c = '^'; System.out.println("Given value = "+c); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
142 Views
To check whether the entered value is ASCII 7-bit numeric, check the character from ‘0’ to ‘9’.Here, we have a numeric character.char one = '9';Now, we have checked a condition with if-else for numeric character from ‘0’ to ‘9’if (c >= '0' && c = '0' && c = '0' && c = '0' && c
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
100 Views
To check whether the entered value is ASCII 7-bit numeric and character (alphanumeric), check the characters ASCII value for −A to Z Or a to z Or 0 to 9Here, we have the following value −char one = '5';Now, we have checked some conditions with if-else for ASCII 7-bit numeric and character.if ((one >= 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
484 Views
Firstly, you need to import the following package for Calendar class in Java.import java.util.Calendar;Create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant. Set a negative value since we are decrementing here.calendar.add(Calendar.WEEK_OF_YEAR, -2);Example Live Demoimport java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime()); // Subtract 2 weeks calendar.add(Calendar.WEEK_OF_YEAR, -2); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
5K+ Views
All distinct elements of an array are printed i.e. all the elements in the array are printed only once and duplicate elements are not printed. An example of this is given as follows.Array = 1 5 9 1 4 9 6 5 9 7 Distinct elements of above array = 1 5 9 4 6 7A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main (String[] args) { int arr[] = {1, 5, 9, 1, 4, 9, 6, 5, 9, 7}; int n = arr.length; ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
884 Views
The array is cyclically rotated clockwise by one. This means that each of the array elements are displayed to the right by one and the last element ends up as the first element. An example of this is given as follows.Original array = 1 2 3 4 5 6 7 8 9 10 Rotated array = 10 1 2 3 4 5 6 7 8 9A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
205 Views
Use the replaceFirst() method to replace only first occurrences of given String with new one.Let’s say we have the following string.String str = "THIS IS DEMO TEXT!";We have to replace the first occurrence of “IS” with “EV”. For that, use replaceFirst() method.str.replaceFirst("IS", "EV");The following is the final example, wherein the first occurrence of “IS” is replaced.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "THIS IS DEMO TEXT!"; System.out.println("String = "+str); System.out.println("Replacing only the first occurrence of substring IS..."); System.out.println("Updated string = ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
292 Views
Let’s say the following is our string.THIS IS DEMO TEXT!Here, to replace every occurrence of ‘I’ with ‘E’, use the replace() method.str.replace('I', 'E'));The following is the complete example to replace all occurrences of a given character with new character.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "THIS IS DEMO TEXT!"; System.out.println("String = "+str); System.out.println("Updated string = "+str.replace('I', 'E')); } }OutputString = THIS IS DEMO TEXT! Updated string = THES ES DEMO TEXT!
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
115 Views
Let’s say the following is our string.String str = "The Walking Dead!";We want to replace the substring “Dead” with “Alive”. For that, let us use the following logic. Here, we have used a while loop and within that found the index of the substring to be replaced. In this way, one by one we have replaced the entire substring.int beginning = 0, index = 0; StringBuffer strBuffer = new StringBuffer(); while ((index = str.indexOf(subStr1, beginning)) >= 0) { strBuffer.append(str.substring(beginning, index)); strBuffer.append(subStr2); beginning = index + subStr1.length(); }The following is the complete example to replace substrings.Example Live Demopublic class ... Read More