hoang nguyen
hoang nguyen

Reputation: 2209

How to using String.split in this case?

I want to write a function like that:

- Input: "1" -> return : "1"
- Input: "12" -> return : ["1","2"]

If I use the function split(): String.valueOf("12").split("") -> ["","1","2"] But, I only want to get the result: ["1","2"].

What the best way to do this?

Infact, I can do that:

private List<String> decomposeQuantity(final int quantity) {
    LinkedList<String> list = new LinkedList<String>();
    int parsedQuantity = quantity;
    while (parsedQuantity > 0) {
        list.push(String.valueOf(parsedQuantity % 10));
        parsedQuantity = parsedQuantity / 10;
    }
    return list;
}

But, I want to use split() for having an affective code

Upvotes: 1

Views: 145

Answers (5)

AnthonyY
AnthonyY

Reputation: 721

public String[] split(String input) {
    if (input.length() == 0) {
        return new String[] { "" };
    }

    String[] output = new String[input.length()];
    int index = 0;
    for (char c : input.toCharArray()) {
        output[index++] = Character.toString(c);
    }

    return output;
}


split("") -> [""]
split("1") - > ["1"]
split("12") - > ["1", "2"]
split("123") - > ["1", "2", "3"]

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

The solution would depend if you want readability and maintainability vs performance. After testing all the posted answers and trying a basic implementation, the best performance was of the basic implementation. This is more code but maybe useful for you:

public interface IEvaluable {
    void evaluate();
}

public class BasicImplementation implements IEvaluable {

    private String string;

    public BasicImplementation(String string) {
        this.string = string;
    }

    //using the power of shadowing
    public String[] getDigitsAsArray(String string) {
        String[] data = new String[string.length()];
        int j = 0;
        for(int i = 0; i < string.length(); i++) {
            if (Character.isDigit(string.charAt(i))) {
                data[j++] = Character.toString(string.charAt(i));
            }
        }
        String[] realData = new String[j];
        for(int i = 0;i < j ;i++) {
            realData[i] = data[i];
        }
        return realData;
    }

    @Override
    public void evaluate() {
        getDigitsByArray(string);
    }
}

To test the performance of the answers, I implemented a method to test the results:

public final class PerformanceEvaluator {

    private IEvaluable evaluable;

    public PerformanceEvaluator(IEvaluable evaluable) {
        this.evaluable = evaluable;
    }

    public void evaluate() {
        long initialTime;
        long endTime;
        initialTime = System.nanoTime();
        try {
            evaluable.evaluate();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        endTime = System.nanoTime();
        System.out.println("Ellapsed time: " + (endTime - initialTime));
    }
}

And the main class

public class Main {

    public static void main(String[] args) {
        Main oMain = new Main();
        oMain.evaluate("12");
        oMain.evaluate("123456789098_)(56765432145");
        oMain.evaluate("wbfiwyfwbn74g0t3h7t394mhg983hhon504n0er0n");
        oMain.evaluate(
            "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
        oMain.evaluate("no digits");
    }

    public void evaluate(String string) {
        PerformanceEvaluator performanceEvaluator =
            new PerformanceEvaluator(new BasicImplementation(string));
        performanceEvaluator.evaluate();
    }
}

Just create other classes that implements the IEvaluable interface with the provided solutions and test yourself. The tests were done in Java 1.7.0_09 HotSpot 64-Bit Server VM on an Intel® Core™ i7-2675QM CPU @ 2.20GHz × 8 with 8 GB RAM over Ubuntu 12.04 LT.

In my opinion based on just these test cases:

  • If you want to use regex and prefer performance, choose @BlueBullet answer.
  • If you want to stick with String#split, choose @Pshemo answer.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124235

To describe place between two digits you can use look-around mechanism, for example:

String[] digits= "12".split("(?<=\\d)(?=\\d)"); //->[1, 2]
//(?<=\\d) points place before digit
//(?=\\d) points place after digit
//(?<=\\d)(?=\\d) place that is before one digit and after another

Although in split you can use only look-behind (?<=\\d) and it will still work

String[] digits= "123".split("(?<=\\d)"); //->[1, 2, 3]

Upvotes: 1

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

You can't do that with just split. You either have to remove the first element (copy to a different array starting from the index 1) or just s.toCharArray() but it will return an array of chars, not strings.

Upvotes: 1

Juvanis
Juvanis

Reputation: 25950

String input = "123";
String regex = "\\d";
Matcher m = Pattern.compile(regex).matcher(input);

String[] output = new String[input.length()];
int index = 0;

while (m.find())
    output[index++] = m.group();

for (String s : output)
    System.out.println(s);

Upvotes: 3

Related Questions