使用竖线字符(“|”)拆分字符串 [英] Splitting string with pipe character ("|")

查看:173
本文介绍了使用竖线字符(“|”)拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从此字符串中拆分值:

I'm not able to split values from this string:

食物1 |服务3 |氛围3 |物有所值1

这是我目前的代码:

String rat_values = "Food 1 | Service 3 | Atmosphere 3 | Value for money 1 ";
String[] value_split = rat_values.split("|");



输出



Output


[,F,o,o,d ,, 1 ,, | ,, S,e,r,v,i,c,e,,3,| ,,, A,t,m,o,s, p,h,e,r,e,,3,| ,, V,a,l,u,e,f,o,r,,m,o,n,e,y ,, 1,]

[, F, o, o, d, , 1, , |, , S, e, r, v, i, c, e, , 3, , |, , A, t, m, o, s, p, h, e, r, e, , 3, , |, , V, a, l, u, e, , f, o, r, , m, o, n, e, y, , 1, ]



预期产出



Expected output


食品1

服务3

气氛3

性价比1

Food 1
Service 3
Atmosphere 3
Value for money 1


推荐答案

| 是正则表达式中的元字符。你需要逃脱它:

| is a metacharacter in regex. You'd need to escape it:

String[] value_split = rat_values.split("\\|");

这篇关于使用竖线字符(“|”)拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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