![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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
204 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)
289 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)
112 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)
312 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)
507 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)
553 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)
336 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