拆分功能无法正常工作 [英] Split function not working properly

查看:47
本文介绍了拆分功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的 Split 函数拆分字符串

I am trying to split the string using Split function in java

String empName="employee name | employee Email";
String[] empDetails=empName.split("|");

它给我的结果是

empDetails[0]="e";
empDetails[1]="m";
empDetails[2]="p";
empDetails[3]="l";
empDetails[4]="o";
empDetails[5]="y";
empDetails[6]="e";
empDetails[7]="e";
.
.
.

但是当我尝试遵循以下代码

but when i try following code

String empName="employee name - employee Email";
String[] empDetails=empName.split("-");

它给了我

 empDetails[0]="employee name ";
 empDetails[1]=" employee Email";

为什么Java split函数无法分割以"|"分隔的字符串?

why java split function can not split the string seperated by "|"

推荐答案

由于 | 是元字符,因此在正则表达式中具有特殊含义.

Since | is a meta character, and it's have a special meaning in regex.

当您逃脱它时就起作用了.

It works when you escape that.

String[] empDetails=empName.split("\\|");

更新:

在java:OFFICIAL DOCS中处理特殊字符.

作为旁注:

在Java方法中,名称以小写字母开头.应为 split()而不是 Split() ..不是大写字母和小号 s

In java method names starts with small letters.it should be split() not Split() ..not the capital and small s

这篇关于拆分功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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