拆分Java String返回空数组? [英] Splitting a Java String return empty array?

查看:189
本文介绍了拆分Java String返回空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似这样的字符串

"myValue"."Folder"."FolderCentury";

我想从点(。)拆分。我尝试使用以下代码:

I want to split from dot("."). I was trying with the below code:

String a = column.replace("\"", "");
String columnArray[] = a.split(".");

但是 columnArray 是空的。我在这里做错了什么?

But columnArray is coming empty. What I am doing wrong here?

我想在这里添加一个可能的字符串数组对象将包含如下所述的spitted值,只有两个对象而不是三个。?

I will want to add one more thing here someone its possible String array object will contain spitted value like mentioned below only two object rather than three.?

columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";


推荐答案

请注意字符串#split 需要正则表达式

您需要转义特殊字符 (这意味着任何字符):

You need to escape the special char . (That means "Any character"):

 String columnArray[] = a.split("\\.");

(逃避正则表达式由 \ ,但在Java中, \ 写成 \\ )。

(Escaping a regex is done by \, but in Java, \ is written as \\).

你也可以使用模式#quote


返回文字模式String 表示String。

String columnArray [] = a.split(Pattern.quote( 。));

通过转义正则表达式,您告诉编译器处理作为字符串 而不是特殊字符

By escaping the regex, you tell the compiler to treat the . as the String . and not the special char ..

这篇关于拆分Java String返回空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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