Topic: 替换字符串中所有某个字符的简单程序

  Print this page

1.替换字符串中所有某个字符的简单程序 Copy to clipboard
Posted by: heartache
Posted on: 2006-03-24 20:18

我的思路:
String str="adsfgtertdddrfsdeftrd";
替换串中所有的d,该为k,有现成的函数方法可以实现吗 ?

2.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-24 20:54

public class testSort
{
public static void main(String[] args)
{
String str=new String("adsfgtertdddrfsdeftrd");
char ch='k';
str.....
...

}
}

3.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: why
Posted on: 2006-03-24 20:57

String
replace(char oldChar, char newChar)
replaceAll(String regex, String replacement)

Regular Expression
java.util.regex (Pattern and Matcher)

4.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-24 22:19

public class testSort2
{
public static void main(String[] args)
{
String str=new String("adsfgtertdddrfsdeftrd");
String ch="k";
str.replaceAll("a",ch);
System.out.println(str);
}
}

5.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-24 22:21

public class testSort2
{
public static void main(String[] args)
{
String str=new String("adsfgtertdddrfsdeftrd");
str.replace('a','k');
System.out.println(str);
}
}

6.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-25 12:04

好象还有个细节没有完成 !

7.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: why
Posted on: 2006-03-26 21:16

heartache wrote:
好象还有个细节没有完成 !

不知所云
請詳明之

8.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: heartache
Posted on: 2006-03-26 22:09

我是说,还是不能得到结果,虽然没有报错,可是结果不理想,所以说应该差点细节

9.Re:替换字符串中所有某个字符的简单程序 [Re: heartache] Copy to clipboard
Posted by: why
Posted on: 2006-03-27 01:13

heartache wrote:
我是说,还是不能得到结果,虽然没有报错,可是结果不理想,所以说应该差点细节

the String class is immutable, so...
str = str.replace('d','K');


   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