在连字符处也拆分字符串 [英] Split string at hyphen characters too

查看:31
本文介绍了在连字符处也拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的行在(.),(),(:)和换行符( \ n)字符.

The line below splits my string on (.), (,), (:) and newline (\n) characters.

String[] textPhrases = text.split("[.,:\\n]");

我也需要分割连字符,但这不起作用:

I need to split on hyphen characters too, but this does not work:

String[] textPhrases = text.split("[.,:/-/\\n]");

我缺少什么或做错了什么?谢谢.

What am I missing or doing wrong? Thank you.

推荐答案

这是正则表达式.连字符在字符类中具有特殊含义.您需要将连字符放置为课程中的第一最后项:

This is regex. A hyphen has a special meaning inside a character class. You need to place the hyphen as the first or last item in the class:

[-.,:\n]

这篇关于在连字符处也拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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