Java Program to split a string using Regular Expression

Samual Sam
Published on 17-Dec-2018 14:06:57
Let’s say we have the following string.String str = "{Java is a programming language} {James Gosling developed it.}";Above, the string is enclosed with parentheses. We can split a string with these parentheses using the split() method.String[] strSplit = str.split("[{}]");The following is an example.Example Live Demopublic class Demo {    public static ... Read More

Java Program to split a string with dot

Samual Sam
Published on 17-Dec-2018 11:29:30
Let’s say the following is our string.String str = "Java is a programming language. James Gosling developed it.";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split("\\.");Above, we have split the string with dot as you can see ... Read More

Split a string in Java

karthikeya Boyini
Published on 17-Dec-2018 11:25:40
Let’s say the following is our string.String str = "This, is, it";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split(", ");Add the result in an array and display it.The following is an example.Example Live Demopublic class Demo { ... Read More

Java Program to display double and single quote in a string

Samual Sam
Published on 17-Dec-2018 11:22:18
The following are our strings with single and double quote.String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!";Above, for single quote, we have to mention it normally like.This is Jack's mobileHowever, for double quotes, use the following and add a slash in the beginning as well ... Read More

Java Program to check if a string is empty or not

karthikeya Boyini
Published on 17-Dec-2018 11:19:09
The following is our string −String str = "";We will check whether it is empty or not using the isEmpty() method. The result will be a boolean −str.isEmpty()The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = ... Read More

Count how many times the substring appears in the larger String in Java

Samual Sam
Published on 17-Dec-2018 11:15:20
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++; ... Read More

Get the substring before the last occurrence of a separator in Java

karthikeya Boyini
Published on 17-Dec-2018 11:12:47
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 ... Read More

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

Samual Sam
Published on 17-Dec-2018 11:09:29
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 ... Read More

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

karthikeya Boyini
Published on 17-Dec-2018 11:04:58
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 = ... Read More

Java Program to format date time with Join

Samual Sam
Published on 17-Dec-2018 11:01:26
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) ... Read More
Advertisements