String.split()---如何将连续分隔符视为一个? [英] String.split() --- How do I treat consecutive delimiters as one?

查看:178
本文介绍了String.split()---如何将连续分隔符视为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于变量temp中的两个示例字符串,例如:

For two sample strings in variable temp such as these:

(1) "|RYVG|111|9|"
(2) "|RYVG|111||9|"

我想做以下事情:

String splitRating[] = temp.split("\\|",);

但我希望结果相同,即:

But I want the result to be the same, which is:

splitrating[0] = ""
splitrating[1] = "RYVG"
splitrating[2] = "111"
splitrating[3] = "9

这意味着我需要将那个双|视为在使用 String.split()

This means that I need to treat that double "|" as one delimiter. Is there any way to do this while still using String.split()?

推荐答案

添加 + 以匹配管道的一个或多个实例:

Add a + to match one or more instances of the pipe:

temp.split("\\|+");

这篇关于String.split()---如何将连续分隔符视为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆