All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Split String In Java

Last Updated : Mar 11, 2024

How To Split String In Java

In this article we will show you the solution of how to split string in java, splitting a string against a given regex returns a char array when called with the java string split() method.

The purpose of this tutorial is to provide examples of the Java String split() method.

In the split() method, the string is divided at the specified regex and an array of substrings is returned.

Splitting a string around regular expression matches is done by the string split() method. We will now discuss the idea of how to split string in java with an example.

Step By Step Guide On How To Split String In Java :-

public String[] split(String regex, int limit) {
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else { // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            if (off == 0)
                return new String[]{this};
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));
            int resultSize = list.size();
            if (limit == 0)
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
                    resultSize--;
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }
  1. In this method, a string is split into substrings based on a number of delimiters (regex). There are two parameters to the function: regex and limit.
  2. A regex parameter specifies how the string will be split, and a limit specifies how many substrings should be returned.
  3. In order to determine whether the delimiter is a single character or an escape sequence, it is checked first. Strings that consist only of a single character will be split using that character. Using the Pattern class, the method constructs a pattern from the escape sequence and splits the string based on that pattern.
  4. In the next step, the method initializes some variables: ch (character), off (offset), next (index of next delimiter), limited (whether to limit how many substrings can be left), and list (a list of substrings).
  5. By using the indexOf() method, the method then searches for the next delimiter in the string. In the case of a delimiter, the method checks whether a limit on the number of substrings is present. In the absence of a limit or a limit that has not been reached, the method adds the substring to the list and updates the offset. Upon reaching the limit, the method adds the remaining part of the string as the last substring and terminates the loop.
  6. If any substrings were added to the list after the loop ended, the method checks whether they were included in the list. The original string is returned if the method does not return the original string. When that does not work, it creates a string array with the same size as the list and copies the substrings into the array. Methods that remove empty substrings from an array when the limit is zero remove all empty substrings.
  7. A string array containing all the substrings is returned by the method as a final result.

Conclusion :-

As a result, we have successfully learned how to split string in java with an example.

String classes define the split() method which breaks strings around regular expression matches by using the regular expression.

Using the split() method in Java, you can break a string depending on the delimiter you've provided.

I hope this article on how to split string in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪