- 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 34487 Articles for Programming
![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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
317 Views
Use the trim() method to remove whitespace from the beginning and end of a string.Let’s say the following is our string with whitespace.str1 = " The Big Bang Theory ";Now, let us trim all the whitespaces from the beginning and the end.String str2 = str1.trim();The following is the final example with output.Example Live Demopublic class Demo { public static void main(String[] args) { String str1 = " The Big Bang Theory "; System.out.println("String: "+str1); String str2 = str1.trim(); System.out.println("Updated string (trimming spaces): "+str2); } }OutputString: The Big ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
510 Views
Use the replace() method to replace a specific character with another. Let’s say the following is our string and here we are replacing a whitespace with a $ character.String str1 = "Orange is the new Black!";Now, use the replace() method to replace a character with $str1.replace(' ', '$');Example Live Demopublic class Demo { public static void main(String[] args) { String str1 = "Orange is the new Black!"; System.out.println("String: "+str1); String str2 = str1.replace(' ', '$'); System.out.println("Updated string: "+str2); } }OutputString: Orange is the new Black! Updated string: ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
31K+ Views
To access character of a string in Java, use the charAt() method. The position is to be added as the parameter.Let’s say we have the following string −String str = "laptop";Let’s find the character at the 4th position using charAt() method.str.charAt(3));The following is the final example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "laptop"; System.out.println("String: "+str); // finding character at 4th position System.out.println("Character at 4th position: "+str.charAt(3)); } }OutputString: laptop Character at 4th position: t
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
560 Views
An array can be reversed upto the given position pos and the remaining array is unchanged. An example of this is given as follows −Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Modified array = 6 5 4 3 2 1 7 8 9 10A 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, 10}; int n = arr.length; int pos ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
A program can be created to check if a string is a correct URL or not. An example of an URL is given as follows −String = https://www.wikipedia.org/ The above string is a valid URLA program that demonstrates this is given as follows.Example Live Demoimport java.net.URL; public class Example { public static boolean check_URL(String str) { try { new URL(str).toURI(); return true; }catch (Exception e) { return false; } } public static void main(String[] args) { String str = "http://www.wikipedia.org/"; System.out.println("String = " + str); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
337 Views
A string is a Heterogram if no letter of the alphabet occurs more than once in it. An example of this is given as follows −String = the big dwarf only jumpsThis string is a Heterogram as each alphabet in the string occurred only once.A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main (String[] args) { String str = "mango"; int n = str.length(); int alphaList[] = new int[26]; int flag = 1; System.out.println("The string is: ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
2K+ Views
Let’s say we have the following string −String myStr1 = "Jack Sparrow";Let us check the string now whether it is not null or not empty.if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty");Example Live Demopublic class Demo { public static void main(String[] args) { String myStr = "Jack Sparrow"; boolean res; if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty"); } else { System.out.println("String is null or empty"); } } }OutputString is not null or not empty
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
864 Views
Each word in a sentence can be reversed and the sentence displayed with the words in the same order as before. An example of this is given as follows −Original sentence = an apple is red Modified sentence = na elppa si derA program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String[] args) { String str = "the sky is blue"; System.out.println("The original string is: " + str); String strWords[] = str.split("\s"); String rev = ""; for(String sw : strWords) { StringBuilder sb = new StringBuilder(sw); sb.reverse(); rev += sb.toString() + " "; ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
2K+ Views
Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, 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 increment the seconds using the calendar.add() method and Calendar.SECOND constant.calendar.add(Calendar.SECOND, 15);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()); // Add 15 seconds to current date calendar.add(Calendar.SECOND, 15); System.out.println("Updated Date = " + calendar.getTime()); } ... Read More