简体   繁体   English

Java将字符串拆分为数组

[英]Java split String into Array

I have a crawler that extracts Data from a website and i am getting the following String: 我有一个从网站提取数据的搜寻器,并且正在获取以下字符串:

    String s = "                 --                 Android 2.3.1 (Gingerbread) --                --                  --                 --                   --                  --                  --                 --                 8" Wide LCD - tela sensível ao toque (resistiva) --                 --                 800 x 600 (4:3) --                --                  --                 --                   --                  --                  --                 --                 1,2 GHz ARM Cortex A8 Core (RK2918) --               --                 4 GB (Memória Flash) e DRAM 512 Mb, DDR3 --                  --                 Slot para cartão Micro SD (Máx. 32 GB) --                --                 Integrado, suporta rotação de tela --                --                 Sim --               --                 Sim --               --                 Suporte a multi idioma: Português, Inglês, Francês, Espanhol, Chinês --                  --                 Navegador para Internet, vídeo, foto e áudio players,e-mail, calculadora, gravador de áudio, suporte a e-book, etc. --               --                   --                  --                  --                 --                 802.11 b/g/n (até 300 Mbps) --               --                 2.1 --               --                 USB 2.0 e Mini USB --                --                   --                  --                  --                 --                 14,65 x 21,50 x 1,45 --                  --                 525g --                   --                  --                 --                 Recarregável, Litium (4700 mAh, 3,7 V) --";

I neet to split that String into one Array but discart the empty ones, so i did this: 我没有将那个String拆分成一个数组,而是分隔空数组,所以我这样做了:

String sr[] = s.split(" -- ");
List<String> list = new ArrayList<String>(Arrays.asList(sr));
list.removeAll(Arrays.asList("", null));

But i keep getting the following result 但我一直得到以下结果

[               ,               Android 2.3.1 (Gingerbread),                 ,               ,              ,                ,               ,               ,              ,               8&quot; Wide LCD - tela sensível ao toque (resistiva),              ,               800 x 600 (4:3),                 ,               ,              ,                ,               ,               ,              ,               1,2 GHz ARM Cortex A8 Core (RK2918),                ,               4 GB (Memória Flash) e DRAM 512 Mb, DDR3,               ,               Slot para cartão Micro SD (Máx. 32 GB),                 ,               Integrado, suporta rotação de tela,                 ,               Sim,                ,               Sim,                ,               Suporte a multi idioma: Português, Inglês, Francês, Espanhol, Chinês,               ,               Navegador para Internet, vídeo, foto e áudio players,e-mail, calculadora, gravador de áudio, suporte a e-book, etc.,                ,                ,               ,               ,              ,               802.11 b/g/n (até 300 Mbps),                ,               2.1,                ,               USB 2.0 e Mini USB,                 ,                ,               ,               ,              ,               14,65 x 21,50 x 1,45,               ,               525g,                ,               ,              ,               Recarregável, Litium (4700 mAh, 3,7 V) --]

I want in the array only stuff thats not empty My guess is thats because the Strings arent really empty and i am getting some HTML blank stuff that i cant get rid. 我只希望数组中的内容不为空我的猜测是,这是因为Strings arent确实为空,并且我得到了一些我无法摆脱的HTML空白内容。

After doing a s.split("\\\\s+(--\\\\s+)+"); 经过s.split("\\\\s+(--\\\\s+)+"); The array is still keeping the empty stuff: 数组仍保留空白:

[, Android 2.3.1 (Gingerbread),  ,  ,  ,  ,  , 8&quot; Wide LCD - tela sensível ao toque (resistiva), 800 x 600 (4:3),  ,  ,  ,  ,  , 1,2 GHz ARM Cortex A8 Core (RK2918), 4 GB (Memória Flash) e DRAM 512 Mb, DDR3, Slot para cartão Micro SD (Máx. 32 GB), Integrado, suporta rotação de tela, Sim, Sim, Suporte a multi idioma: Português, Inglês, Francês, Espanhol, Chinês, Navegador para Internet, vídeo, foto e áudio players,e-mail, calculadora, gravador de áudio, suporte a e-book, etc.,  ,  ,  , 802.11 b/g/n (até 300 Mbps), 2.1, USB 2.0 e Mini USB,  ,  ,  , 14,65 x 21,50 x 1,45, 525g,  ,  , Recarregável, Litium (4700 mAh, 3,7 V) --]

You can try this: 您可以尝试以下方法:

String sr[] = s.split("\\s+--\\s+");

Putting "\\\\s+" will take in an arbitrary number of spaces, instead of just " " which is just one space (if you want just the space character to be taken into account, replace \\\\s with a litteral space character). "\\\\s+"将在空间任意数,而不是仅仅" "这仅仅是一个空间(如果你只想要space字符加以考虑,更换\\\\s与litteral space字符)。 If you want to avoid all emply elements in the array, try: 如果要避免数组中所有emply元素,请尝试:

String sr[] = s.split("\\s+(--\\s+)+");

Having (--\\\\s+)+ means that even if the pattern is repeated, it removes them all. 具有(--\\\\s+)+表示即使重复模式,也将全部删除。

I think what you are looking for is String.replace() : 我认为您正在寻找的是String.replace()

String sentence = "Hello World   !";
String str = sentence.replace(" ", "");

System.out.println(str);

Output: 输出:

HelloWorld!

您可以在数组中的字符串上调用String#trim() ,这将删除所有空白。

To remove all the empty strings and those that only contain whitespace from the list: 要从列表中删除所有空字符串和仅包含空格的字符串:

Iterator<String> it = list.iterator();
while (it.hasNext()) {
    String s = it.next();
    if (s.matches("^\\s*$")) {
        it.remove();
    }
}

Try this: 尝试这个:

    String sr[] = s.split("--");
    List<String> list = new ArrayList<String>(Arrays.asList(sr));
    ArrayList<String> removeList = new ArrayList<String>();
    String curr;
    for (int i=0; i < list.size(); i++) {
        curr = list.get(i).trim();
        list.set(i, curr);
        if (curr.length() == 0)
            removeList.add(curr);
    }
    list.removeAll(removeList);
    System.out.println(list);
ArrayList<String> result = new ArrayList<String>();
String entries[] = s.split("--");
for(String entry:entries){
  String noSpace = entry.replaceAll(" ","");
  if(!noSpace.isEmpty()){
    result.add(noSpace);
  }
}
return result;

The String gets split by "--", then each element is added to the result, except when it only contains whitespaces. 字符串用“-”分割,然后将每个元素添加到结果中,除非它仅包含空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM