0

Say we would like to write a method to receive entire book in a string and an arbitrary single-character delimiter to separate strings and return an array of strings. I came up with the following implementation (Java).(suppose no consecutive delimiter etc)

ArrayList<String> separater(String book, char delimiter){
ArrayList<String> ret = new ArrayList<>();
String word ="";

    for (int i=0; i<book.length(), ++i){
        if (book.charAt(i)!= delimiter){
            word += book.charAt(i);
        } else {
            ret.add(word);
            word = "";
        }
    }
    return ret;
}

Question: I wonder if there is any way to leverage String.split() for shorter solutions? Its because I could not find a general way of defining a general regex for an arbitrary character delimiter.

String.split("\\.") if the delimiter is '.'
String.split("\\s+"); if the delimiter is ' ' // space character 

That measn I cold not find a general way of generating the input regex of method split() from the input character delimiter. Any suggestions?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • ??? so which language do you want the answers to refer to? – alvas Jan 11 '14 at 23:14
  • What's wrong with calling book.split(""+delimiter)? Maybe I'm not understanding the question fully – Alejandro Jan 11 '14 at 23:15
  • @user1274223 I think book.split(""+delimiter) would not work for space character or back slash. I am looking for a general approach – C graphics Jan 11 '14 at 23:21
  • possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – alvas Jan 11 '14 at 23:22
  • you can specify the split delimiter with `String string = "004-034556"; String[] parts = string.split("-");` – alvas Jan 11 '14 at 23:23
  • 3
    @alvas please pay more attention, I am looking for a general case where the delimiter is any arbitrary character. – C graphics Jan 11 '14 at 23:30

1 Answers1

2
String[] array = string.split(Pattern.quote(String.valueOf(delimiter)));

That said, The Guava Splitter is much more versatile and well-behaving than String.split().

And a note on your method: concatenating to a String in a loop is very inefficient. As Strings are immutable, it produces a lot of temporary Strings and StringBuilders. You should use a StringBuilder instead.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I totally agree with you with regard using the Strings instead of StringBuilder, I use string just for simplicity. but a funny this is that an Amazone interviewer believed the java compiler will take care of it!! – C graphics Jan 11 '14 at 23:32
  • The compiler does take care of it for code such as `String s = "hello" + title + " " + firstName + " " + lastName;`. But not for appends in loops. – JB Nizet Jan 11 '14 at 23:34
  • I just got the point about your advice of using Guava Splitter ( which btw I dont know what that is) as your solution would not work for backslash '\\' or even string.split(Pattern.quote("\\")) and raises java.util.regex.Pattern.error(Unknown Source) error – C graphics Jan 12 '14 at 00:07
  • By Guava Splitter, he probably means: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Splitter.html – Dennis Meng Jan 12 '14 at 00:28