
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

409 Views
In this article, we will learn how to check if a String is empty in Java. The isEmpty() method is used to check whether a given string does not contain characters. Problem StatementGiven a string, write a Java program to determine if the string is empty.Input String str = ""Output String is empty: true Steps to check if a string is emptyThe following are the steps to check if a string is empty − Create a string. Use the isEmpty() method to check whether the string has no characters. ... Read More

360 Views
Let’s say we have the following string.String str = "Learning never ends! Learning never stops!";In the above string, we need to find out how many times the substring “Learning” appears.For this, loop until the index is not equal to 1 and calculate.while ((index = str.indexOf(subString, index)) != -1) { subStrCount++; index = index + subString.length(); }The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "Learning never ends! Learning never stops!"; System.out.println("String: "+str); int subStrCount = 0; String subString ... Read More

6K+ Views
We have the following string with a separator.String str = "David-Warner";We want the substring before the last occurrence of a separator. Use the lastIndexOf() method.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.lastIndexOf(separator); System.out.println("Substring before last separator = "+str.substring(0, sepPos));The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "David-Warner"; String separator ="-"; int sepPos = str.lastIndexOf(separator); if (sepPos == -1) { System.out.println(""); ... Read More

1K+ Views
We have the following string with two separators.String str = "Tom-Hank-s";We want the index of the first occurrence of the separator.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.indexOf(separator);The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "Tom-Hank-s"; String separator ="-"; System.out.println("String: "+str); int sepPos = str.indexOf(separator); System.out.println("Separator's first occurrence: "+sepPos); } }OutputString: Tom-Hank-s Separator's first occurrence: 3

9K+ Views
We have the following string with a separator.String str = "Tom-Hanks";We want the substring after the first occurrence of the separator i.e.HanksFor that, first you need to get the index of the separator and then using the substring() method get, the substring after the separator.String separator ="-"; int sepPos = str.indexOf(separator); System.out.println("Substring after separator = "+str.substring(sepPos + separator.length()));The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = "Tom-Hanks"; String separator ="-"; int sepPos = str.indexOf(separator); if (sepPos == -1) ... Read More

342 Views
To format date time with Join, set the date as a string and do not forget to add the delimeter.For delimeter “/” in the dateString.join("/","11","11","2018");For delimeter “:” in the date.String.join(":", "10","20","20");The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String d = String.join("/","11","11","2018"); System.out.print("Date: "+d); String t = String.join(":", "10","20","20"); System.out.println("Time: "+t); } }OutputDate: 11/11/2018 Time: 10:20:20

1K+ Views
To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as shown below −String.join("$","Demo","Text");The following is an example.Example Live Demopublic class Demo { public static void main(String[] args) { String str = String.join("$","Demo","Text"); System.out.println("Joined strings: "+str); } }OutputJoined strings: Demo$Text

1K+ Views
For a given string, say "str", write a Java program to check if it contains each character from a specified character array. If it contains print "found" otherwise "not found". A String in Java is a class that represents a contiguous block of characters. Example Scenario: Input 1: String str = "pqrst"; Input 2: char[] chSearch = {'p', 'q', r'}; Output: res = Character p, q and r found in given string In the string "pqrst", we are searching for the character set {'p', 'q', r'}. Using Iteration Here, use for loop to iterate till the length of the ... Read More

966 Views
In this article, we will learn to check if the string contains any character in the given set of characters in Java. We will iterate over the string and compare each character to a set of characters that we want to search for. If any match is found, the program will print the character and confirm that it is present in the string. Problem Statement Write a program in Java to check if the string contains any character in the given set of characters − Input str = abcde chSearch = 'b', 'c' Output Character b found in string abcde ... Read More

2K+ Views
Let’s say the following is our string.String myStr = "";Now, we will check whether the above string is whitespace, empty ("") or null.if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) { System.out.println("String is not null or not empty or not whitespace"); } else { System.out.println("String is null or empty or whitespace"); }The following is an example that checks for an empty string.Example Live Demopublic class Demo { public static void main(String[] args) { String myStr = ""; if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) { System.out.println("String is not null ... Read More