Found 9316 Articles for Object Oriented Programming

Get the index of the first occurrence of a separator in Java

Samual Sam
Updated on 27-Jun-2020 06:46:57

791 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

Get the substring after the first occurrence of a separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:47:47

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

Java Program to format date time with Join

Samual Sam
Updated on 27-Jun-2020 06:31:35

257 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

Join Strings in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:32:26

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

Java Program to check if the String contains only certain characters

Samual Sam
Updated on 27-Jun-2020 06:33:25

993 Views

The following is our string.String str = "pqrst";In the above string, we want to search for the following set of characters.// set of characters to be searched char[] chSearch = {'p', 'q', r'};For this, loop through the length of the string and check every character in the string “str”. If the matching character from “chSearch” is found in the string, then it would be a success.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "pqrst";       // characters to be searched       char[] ... Read More

Java Program to check if the String contains any character in the given set of characters

karthikeya Boyini
Updated on 27-Jun-2020 06:34:27

593 Views

Here, we have a string.String str = "abcde";In the above string, we want to search for the following set of characters.// set of characters to be searched char[] chSearch = {'b', 'c'};For this, loop through the length of the string and check every character in the string str. If the matching character from “chSearch” is found in the string, then it would be success.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "abcde";       // set of characters to be searched       char[] ... Read More

Check if a String is whitespace, empty ("") or null in Java

Samual Sam
Updated on 27-Jun-2020 06:35:40

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

Remove newline, space and tab characters from a string in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:36:38

16K+ Views

To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String originalStr = "Demo\tText";       System.out.println("Original String with tabs, spaces and newline: "+originalStr);       originalStr = originalStr.replaceAll("[\t ]", "");       System.out.println("String after removing tabs, spaces and new line: "+originalStr);    } }OutputOriginal String with tabs, spaces and newline: Demo Text ... Read More

Display Month in MMMM format in Java

Samual Sam
Updated on 27-Jun-2020 06:37:32

9K+ Views

The MMMM format for months is like entire month name: January, February, March, etc. We will use it like this.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMMM format = "+strMonth);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();     ... Read More

Display Month in MMM format in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:38:45

4K+ Views

The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc. Here, we will use the following.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMM format = "+strMonth);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();   ... Read More

Advertisements