String 的 split …

来源:互联网 发布:免费网络会议室 编辑:程序博客网 时间:2024/04/28 21:02
Problem

转载自:http://www.javagalaxy.com/forum/viewtopic.php?p=377&sid=2d2152ff1ee06bc37c31b4b40e9a6fad

String str = "testing??";

str = str.replaceAll("?", "");

When you use the above pattern you get an exception as "Danglingmeta character '?' near index 0"


Solution

String str = "testing??";

str = str.replaceAll("\\?", "");

You can use the above solution when you are replacing * and +symbols

String str = "testing??*+";

str = str.replaceAll("\\*", ""); //"Dangling meta character '*'near index 0"
str = str.replaceAll("\\+", ""); //"Dangling meta character '+'near index 0"

+、*、|、\等符号在正则表达示中有相应的不同意义。
一般来讲只需要加[]、或是\\即可

int i=s.split("[?]").length;

或者

int i=s.split("\\?").length;i);

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zuoanlove/archive/2011/05/06/6400887.aspx

0 0
原创粉丝点击