Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除java中的空值,如(,,test,tester,,,java_test,,,,)_Java - Fatal编程技术网

如何删除java中的空值,如(,,test,tester,,,java_test,,,,)

如何删除java中的空值,如(,,test,tester,,,java_test,,,,),java,Java,我的字符串如下:,,test,tester,,java\u test,,,。我需要从列表中删除空值。你能分享一下你的想法吗 预期结果:测试、测试员、java_测试 删除所有空元素和空元素。首先删除双逗号: my_string = my_string.replaceAll("(,)+", ","); 去掉前导逗号: my_string = my_string.replaceAll("^,", ""); my_string = my_string.replaceAll(",$", ""); 去

我的字符串如下:,,test,tester,,java\u test,,,。我需要从列表中删除空值。你能分享一下你的想法吗

预期结果:测试、测试员、java_测试


删除所有空元素和空元素。

首先删除双逗号:

my_string = my_string.replaceAll("(,)+", ",");
去掉前导逗号:

my_string = my_string.replaceAll("^,", "");
my_string = my_string.replaceAll(",$", "");
去掉尾随逗号:

my_string = my_string.replaceAll("^,", "");
my_string = my_string.replaceAll(",$", "");
总而言之:

my_string = my_string.replaceAll("(,)+", ",").replaceAll("^,", "").replaceAll(",$", "")
如果括号构成字符串的一部分,解决方案如下:

my_string = my_string.replaceAll("(,)+", ",").replaceAll("^\\(,", "(").replaceAll(",\\)$", ")")

那个列表是字符串还是真实的列表?对不起,列表是字符串、、测试、测试、java_测试、、通过循环检查列表中的每个元素。在String类中使用isEmpty内置函数。若列表是一个字符串,通过split,通过逗号字符分割字符串,然后逐个检查。对于列表,您是否可以执行list.removiefn->n.equals;你是对的:-编辑后,现在从列表中删除所有内容。n->!=null意味着删除所有不为null的内容。OP已经用更多信息更新了帖子,他们的列表实际上是stringWelcome to StackOverflow!只有代码的答案是低质量的答案。加上一点解释。此代码为什么/如何工作?