質問

I have the following file format:

bla_english bla_german eok This is the description part

The columns are: bla_english, bla_german, eok and "This is the description part". The first three columns have only one word. The description may have more than one words and is optional. What is the best way to parse that file with google guava?

役に立ちましたか?

解決

Splitter.on(' ').limit(4).split(input);

This uses Guava and your processor will be happy to not have to deal with the regular expressions, unlike input.split("\\s", 4).

If you need a broader space matching, you can use

Splitter.on(CharMatcher.WHITESPACE).limit(4).split(input);

Or you can create your own CharMatcher, of course.

But what I'd really do is define the Splitter in my class (static) or instance (non static) and then use split on it. So you only have to define it once and it will be ready for each call you make!

class MyClass {
  static Splitter splitter = Splitter.on(CharMatcher.WHITESPACE).limit(4);

  ...

  Iterable<String> slices = splitter.split(input);
}

他のヒント

If there are at most 4 columns then you can simply do:

final String[] columns = input.split("\\s", 4);

From the Javadoc:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

If you have fewer than 4 columns then columns.length will simply be the number of columns. If you have more than 4 columns then the remainder, after extracting the first 3, will be dumped into the fourth column.

No need for Guava.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top