简体   繁体   English

“”作为结果进入 java 与正则表达式拆分

[英]“” as result into java split with Regexp

I have string content and need to split into an array of tokens, but one of the tokens as a result is "", which can result in multiple tokens like "" and I need to avoid them by Regexp.我有字符串内容,需要拆分成一个标记数组,但是其中一个标记是“”,这可能会导致多个标记,如“”,我需要通过正则表达式来避免它们。

I try use the Regexp like the print but he do not remove my problem.我尝试像打印一样使用正则表达式,但他没有解决我的问题。

Node content example:节点内容示例:

Regexp and the result:正则表达式和结果:

You are splitting your string on spaces (among various other characters).您正在将字符串拆分为空格(在各种其他字符中)。

You'd be better off if(node.equals("")){// ignore it or remove it} because whatever you split your string on, you will always have to worry about empty results because your split character could be anywhere in the string.你最好if(node.equals("")){// ignore it or remove it}因为无论你分割你的字符串,你总是需要担心空结果,因为你的分割字符可能在任何地方字符串。 Calling trim on your string before you split it will get rid of all that extra leading and trailing space and, because it's spaces you're splitting on, get rid of those pesky empty values;在拆分之前在字符串上调用 trim 将消除所有额外的前导和尾随空格,并且因为它是您要拆分的空格,所以摆脱那些讨厌的空值; which from what I can see in your question, is exactly what's going on.从我在你的问题中可以看到,这正是正在发生的事情。

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

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