gpt4 book ai didi

java - omitEmptyStrings 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 16:03:38 26 4
gpt4 key购买 nike

我被困在中间了。

我正在创建包装类com.google.common.base.Splitter。

但是omitEmptyStrings方法无法正常工作。

请给我你的精彩指导。

这是[来电者]

public class Hoge {
public static void main(String[] args) {
Iterable<String> hoge =
Splitter.on(",")
.omitEmptyStrings()
.split(" foo ,,11,bar1 , quux");

}
}

这是结果:[福福]
[11]
[栏1]
[★quux]

下面是我的包装类。

public final class Splitter extends Object {

private static final Splitter splitter = new Splitter();

private static com.google.common.base.Splitter guavaSplitter;

private static Object separatorChars = null;

private static boolean omitEmptyStringsFlg = false;

private static CharMatcher trimmer = null;

/**
* Default Construction
* This constructor is private to deny access
*
*/
private Splitter() {
}
/**
* setting guava Splitter to this class guavaSplitter
*
* @param separatorMatcher
* @return Splitter
*/
public static Splitter on(final String separator) {

if (StringUtils.isEmpty(separator)) {
Splitter.guavaSplitter = null;
} else {
Splitter.guavaSplitter
= com.google.common.base.Splitter.on(separator);

Splitter.separatorChars = separator;
}

return Splitter.splitter;

}

/**
* setting guava Splitter to this class guavaSplitter
*
* @return Splitter
*/

public Splitter omitEmptyStrings() {

if (null != Splitter.guavaSplitter) {

if (Splitter.separatorChars instanceof String) {
if (null != Splitter.trimmer) {
Splitter.guavaSplitter
= com.google.common.base.Splitter.on(
(String)Splitter.separatorChars)
.omitEmptyStrings()
.trimResults(Splitter.trimmer);

} else {
Splitter.guavaSplitter
= com.google.common.base.Splitter.on(
(String)Splitter.separatorChars).omitEmptyStrings();

}

}

}
Splitter.omitEmptyStringsFlg = true;
return Splitter.splitter;
}

/**
* Splits the {@link CharSequence} passed in parameter.
*
* @param sequence the sequence of characters to split
* @return an iteration over the segments split from the parameter.
*/
public Iterable<String> split(final CharSequence sequence) {
Iterable<String> retList = new List();

if (null != sequence && null != Splitter.guavaSplitter) {
retList = Splitter.guavaSplitter.split(sequence);

}

return retList;
}

}


/**
*
* Returns an iterator over a set of elements of type String.
*
*/
class List implements Iterable<String> {

private java.util.List<String> splittedList
= new java.util.ArrayList<String>();

@Override
public Iterator<String> iterator() {
return this.splittedList.iterator();
}
}

最佳答案

看起来除了 omitEmptyStrings 之外,您还想要 trimResults,不是吗?

关于java - omitEmptyStrings 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com