English 中文(简体)
每一要素的组合
原标题:Split string and trim every element
  • 时间:2011-11-02 20:51:40
  •  标签:
  • java

是否存在任何图书馆APIC或regex型号,以分立一些划定者,并自动地从每个要素引领和跟踪空间,而不必穿透这些要素?

例如,在分批<代码>上,A B # C#D# E#“ on #上,预期产出为[A、C、D、E]

最近的I got是split(”/s*#/s*> , 登上[A、C、D、E]

最佳回答

坐在你分裂之前

" A B # C#D# E # ".trim().split("\s*#\s*")

<代码>后天线的空间[A、C、D、E]只是<代码>。

问题回答

Guava到救援! Use CharMatcherJoiner, only to itching the Iterable Backjoint, clear show that the iterable only has the letter in it, with no pa, extraneousspace, or hash sign.

package main;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

public class TestMain {

    static Splitter split = Splitter.on(CharMatcher.anyOf(" #")).trimResults()
            .omitEmptyStrings();
    static Joiner join = Joiner.on(", ");

    public static void main(String[] args) {
        final String test = " A B # C#D# E # ";
        System.out.println(join.join(split.split(test)));
    }
}

产出:

A, B, C, D, E

那些从小管处获得头痛的人大。

为什么在分裂之前做一个替代物呢?

str.replaceall("\s*#\s*","#").split()

Edited to correct whitespace error that was pointed out by Marcus.

我认为,适当的规范应当是:

str.split("[\s]*#[\s]*");

http://regexpal.com/“rel=“nofollow”http://regexpal.com/。

否则,它就应当如此:

"  A   B #  C#D# E  #  "
    .split( # )
    .map(function(item) { return item.trim(); } )
    .filter(function(n){ return n != "" });

outputs: ["A B", "C", "D", "E"]





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签