Topic: 字符串的替换问题:如何能不区分大小的替换。 |
Print this page |
1.字符串的替换问题:如何能不区分大小的替换。 | Copy to clipboard |
Posted by: myblessu Posted on: 2003-12-30 22:11 <%@ page contentType="text/html; charset=GB2312" %> <%@ page language="java" %> <% String str1="The apache Software Foundation"; String str2="The Apache Software Foundation"; String key="apache"; out.print (str1.replaceAll(key,"<font color=red>"+key+"</font>")); // 当str1经过上面的替换后,将输出: // The <font color=red>apache</font> Software Foundation // 请问如果能使 str2 根据 key 的内容(不区分大小写)替换后,输出: // The <font color=red>Apache</font> Software Foundation // 注意 key 及 str2的大小写无规律。 %> |
2.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: mochow Posted on: 2003-12-31 09:35 可以自己写一个类吧, 重载eqauls方法使其对大小写不敏感 由于String 是final所以不能从其继承。 |
3.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: looluo Posted on: 2003-12-31 11:35 String的replaceAll方法第一参数是一个regular expression,所以写出一个不区分字符串大小写的正规表达式就可以 具体怎么写,我现在还不知道:) |
4.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: zuohaosh Posted on: 2003-12-31 16:55 Pattern p = Pattern.compile(key, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str1); m.replaceAll("<font color=red>"+key+"</font>"); |
5.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: zuohaosh] | Copy to clipboard |
Posted by: myblessu Posted on: 2004-01-02 08:58 请问:zuohaosh 需要Pattern时,需要导入哪个软件包?谢谢! |
6.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: dingligang Posted on: 2004-01-02 10:16 /*since jdk 1.4*/ |
7.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: dingligang] | Copy to clipboard |
Posted by: myblessu Posted on: 2004-01-02 11:16 请教 zuohaosh 与 dingligang <%@ page contentType="text/html; charset=GBK" %> <%@ page import = "java.util.regex.*"%> <% String key="apache"; String str1="the apache ahe ApAche theapache"; Pattern p = Pattern.compile(key, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str1); m.replaceAll("<font color=red>"+key+"</font>"); out.print (m.toString()); %> 可是输出结果是:java.util.regex.Matcher@1b9d4d8 呀! 并不是我真正想要的: the <font color=red>apache</font> ahe <font color=red>ApAche</font> the<font color=red>apache</font> 这怎么使用呢? |
8.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: wsj Posted on: 2004-01-02 11:54 out.print(m.replaceAll("<font color=red>"+key+"</font>")); |
9.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: wsj] | Copy to clipboard |
Posted by: myblessu Posted on: 2004-01-02 12:38 嗯,谢谢各位了,虽然还没有完全达到我的原来的要求,但这也基本可以了. 比如原本我是想: 在 key="apache" ,str="apaChE" 时 替换后是 <font color=red>apaChE</font> . 而不是 <font color=red>apache</font>. |
10.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: myblessu] | Copy to clipboard |
Posted by: wsj Posted on: 2004-01-02 13:59 try: out.print(m.replaceAll("<font color=red>$0</font>")); |
11.Re:字符串的替换问题:如何能不区分大小的替换。 [Re: wsj] | Copy to clipboard |
Posted by: myblessu Posted on: 2004-01-02 14:12 大功告成,谢谢各位,谢谢wsj |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |