0

I want to split a string along several different conditions - I understand there is a Java String method called String.split(element), which splits the String into an array based on the element specified.

However, splitting among more objects seems to be very complex -- especially if the split must occur to a range of elements.

Precisely, I want java to split the string

  • "a>=b" into {"a",">=","b"}

  • "a>b" into {"a", ">", "b"}

  • "a==b" into {"a","==","b"}

I have been fiddling around with regex too just to see how to split it exactly based on this parameters, but the closest I've gotten is just splitting along a single character.

EDIT: a and b are arbitrary Strings that can be of any length. I simply want to split along the different kinds of comparators ">",">=","==";

For example, a could be "Apple" and b could be "Orange".

So in the end I want the String from "Apple>=Orange" into {"Apple", ">=", "Orange"}

19
  • 1
    What are the conditions for the split?
    – AntonH
    Feb 23, 2017 at 21:18
  • The conditions I specified are under what I want precisely.
    – Kevin Hu
    Feb 23, 2017 at 21:19
  • Why not rewrite your strings as a >= b, a < b and then split on " "?
    – SedJ601
    Feb 23, 2017 at 21:20
  • 2
    You haven't specified any conditions, you just included some sample inputs and outputs.
    – shmosel
    Feb 23, 2017 at 21:21
  • I am taking in an input that could both have whitespace and not have whitespace - in my case, I already considered the cases with whitespace
    – Kevin Hu
    Feb 23, 2017 at 21:22

3 Answers 3

1

You can use regular expressions. No matter if you use a, or b or abc for your variables you'll get the first variable in the group 1, the condition in the group 2 and the second variable in the group 3.

    Pattern pattern = Pattern.compile("(\\w+)([<=>]+)(\\w+)");
    Matcher matcher = pattern.matcher("var1>=ar2b");

    if(matcher.find()){
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
    }
5
  • Perfect. How does regex know the difference between >= and >, however? I've been trying to learn it for days and still don't understand how it identifies ">" differently from ">="
    – Kevin Hu
    Feb 23, 2017 at 21:34
  • your regex will capture much more than what's intended
    – niceman
    Feb 23, 2017 at 21:36
  • 1
    I prefer this regex instead : "(\\w+)"+(compop)+"(\\w+)" where compop=">|<|==|<=|>="
    – niceman
    Feb 23, 2017 at 21:41
  • 2
    @KevinHu while this answer works, pay attention that it captures much more than what you want example : "hello>>no" will still match and you'll get {"hello",">>","no"}
    – niceman
    Feb 23, 2017 at 21:43
  • Yes, it's better to limit the options, if you are only using >, <, ==, <= and >= it's better to use this regex "(\\w+)(>|<|==|<=|>=)(\\w+)"
    – reos
    Feb 23, 2017 at 22:11
0

The following code works for your examples:

System.out.println(Arrays.asList("a<=b".split("\\b")));

It splits the string on word boundaries.

If you need more elaborate splitting, you have to provide more examples.

0
0

You could code it out by hand and use whichever tokens you want to split on like so

public String[] splitString(String word)
{
    String[] pieces;
    String[] tokens = {"==", ">=", "<=","<", ">"};
    for(int i = 0; i < tokens.length; i++)
    {
        if(word.contains(tokens[i]))
        {
            pieces = {
                    word.substring(0, word.indexOf(tokens[i])), 
                    tokens[i], 
                    word.substring(word.indexOf(tokens[i]) + 
                      tokens[i].length(), word.length())};
            return pieces;
        }
    }
    return pieces;
}

This will return an array with whatever is before the token found, the token itself and whatever is left.

1
  • a ?!! don't you mean word ?
    – niceman
    Feb 23, 2017 at 21:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.