Question

In: Computer Science

The split method in the String class returns an array of strings consisting of the substrings...

The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters.
public static String[] split(String s, String regex)
For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?, gf, #, and e in an array of String.
Write a test program to show that your split method works properly.

PLEASE DO THE QUESTION IN A SIMPLER WAY

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

SubstringOperator.java

package c15;

import java.util.Arrays;

public class SubstringOperator {

   public static String[] split(String s, String regex) {
       int count=0;
       for(int i=0;i<s.length();i++) {
           if(regex.indexOf(s.charAt(i))!=-1) {
               count++;
           }
       }
       int size = count*2+1;
       String res[] = new String[size];
       String tmp;
       int storeCount=0;
       for(int i=0;i<s.length();i++) {
           int index=regex.indexOf(s.charAt(i));
           if(index!=-1) {
               tmp = s.substring(0,i);
               res[storeCount] = tmp;
               storeCount++;
               res[storeCount] = s.charAt(i)+"";
               s = s.substring(i+1);
               storeCount++;
               i=0;
           }
       }
       res[storeCount] = s;
       return res;
   }
   public static void main(String[] args) {
       String res[] = split("a?b?gf#e", "[?#]");
       System.out.println(Arrays.toString(res));
      
       String res2[]=split("ab#12#453", "#");
       System.out.println(Arrays.toString(res2));
      
   }
}

output


Related Solutions

Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Create an array of ten strings. Write a program to join and split these strings. Feel...
Create an array of ten strings. Write a program to join and split these strings. Feel free to use any whitespace character. Explain how the program works, when to use Python arrays, and when to use tuples, dictionaries, and sets. Explain how the program works and how it can be utilized to support an organization’s requirements.
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
write a java prog that include an array of strings of size 50    String[] strings...
write a java prog that include an array of strings of size 50    String[] strings = new String[50]; then find the max length of the inputs inside a method keep reading from the user until user enters keyword to stop input : may ala jony ram asd fgghff daniel jwana output : max length : 10
1.        In your program, demo the split() method for strings and attach it to one of the...
1.        In your program, demo the split() method for strings and attach it to one of the buttons. "Numbers" lesson 2.        Check if 3===3.0. Why? 3.        Find the number of zeros such that 3===3.000…01. Why can this happen? 4.    If var x=123e15, and var y=123e-15, does x===x+y? why? 5.    What is typeof NaN? Comment on its (il)logicality. "Number Methods" lesson 6.    What is the hexadecimal number ff (written in JavaScript as 0xff) in base 10? 7.    Write all the numbers from 0 to 20 in hexadecimal form....
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
: This exercise will give you a review of Strings and String processing. Create a class...
: This exercise will give you a review of Strings and String processing. Create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT