String.split("。")没有拆分我的长字符串 [英] String.split(".") is not splitting my long String

查看:89
本文介绍了String.split("。")没有拆分我的长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做以下事情:

String test = "this is a. example";
String[] test2 = test.split(".");

问题: test2 没有项目。但是测试中有许多 字符串

the problem: test2 has no items. But there are many . in the test String.

知道问题是什么吗?

推荐答案

注意 public String [] split (字符串正则表达式)需要正则表达式

您需要转义特殊字符

使用 String [] test2 = test.split(\\。);

现在你告诉Java:

不要把作为特殊字符,将其作为常规字符

"Don't take . as the special char ., take it as the regular char .".

请注意,转义正则表达式是由 \ 完成的,但在Java中, \ 写为 \\

Note that escaping a regex is done by \, but in Java, \ is written as \\.

正如@OldCurmudgeon(+1)的评论,您可以使用 public static String quote(String s)返回文本模式指定String的字符串

As suggested in the comments by @OldCurmudgeon (+1), you can use public static String quote(String s) that "Returns a literal pattern String for the specified String":

String [] test2 = test.split(Pattern.quote(。));

这篇关于String.split("。")没有拆分我的长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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