Split a String Using Regular Expression in Java

Samual Sam
Updated on 27-Jun-2020 06:51:09

1K+ Views

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 void main(String[] args) throws Exception {       String str = "{Java is a programming language} {James Gosling developed it.}";       String[] strSplit = str.split("[{}]");       System.out.println("Splitting String...");       for (int i = 0; i < strSplit.length; i++)       System.out.println(strSplit[i]);   ... Read More

Perform Merge Sort Using C#

Arjun Thakur
Updated on 27-Jun-2020 06:50:48

2K+ Views

Merge Sort is a sorting algorithm that uses the divide and conquer method. It divides the array into two parts and then calls itself for each of these two parts. This process is continued until the array is sorted.A program that demonstrates merge sort in C# is given as follows −Example Live Demousing System; namespace QuickSortDemo {    class Example {       static public void merge(int[] arr, int p, int q, int r) {          int i, j, k;          int n1 = q - p + 1;          int ... Read More

Get Substring After First Occurrence of a Separator in Java

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

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

Get the Index of the First Occurrence of a Separator in Java

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

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

Get Substring Before Last Occurrence of a Separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:46:18

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

Draw Close Shape in Android

Ankith Reddy
Updated on 27-Jun-2020 06:46:17

460 Views

 This example demonstrate about How to draw close shape in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken imageview and added background as background.xml.Step 3 − Add the following code to drawable/ background.xml     Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of ... Read More

Count Substring Occurrences in a Larger String in Java

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

365 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

Create Circular EditText in Android

George John
Updated on 27-Jun-2020 06:45:32

168 Views

This example demonstrate about How to create circler edit text in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken edit text and added background as background.xml.Step 3 − Add the following code to drawable/ background.xml             Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the ... Read More

Add Image on EditText in Android

Arjun Thakur
Updated on 27-Jun-2020 06:44:58

601 Views

This example demonstrate about how can I add an image on EditText.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken edit text and added background as background.xml.Step 3 − Add the following code to drawable/ background.xml             Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the ... Read More

Display Double and Single Quote in a String in Java

Samual Sam
Updated on 27-Jun-2020 06:44:16

5K+ Views

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 as at the end.String str2 = "\"This is it\"!";The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str1 = "This is Jack's mobile";       String str2 = "\"This is it\"!";       System.out.println("Displaying Single Quote: "+str1); ... Read More

Advertisements