Topic: 【求助】关于字符串处理的几个问题:

  Print this page

1.【求助】关于字符串处理的几个问题: Copy to clipboard
Posted by: applelfish
Posted on: 2005-01-10 15:26

1 如何将字符串的首字母和尾字母转化成大写
例如: myproject变成MyprojecT
2 如何将字符串中的所有a替换成*
例如: mynameisapple变成myn*meis*pple
3 如果字符串中包含子串src,如何取得第一个子串src后的所有字符
例如: D:\myproject\src\a\b\src取出a\b\src

2.Re:【求助】关于字符串处理的几个问题: [Re: applelfish] Copy to clipboard
Posted by: lele
Posted on: 2005-01-10 16:47

substring和replace方法,代码写的不好,很笨,希望对你有参考价值

public class TestSubstring {

public static void main(String[] args) {
//1 如何将字符串的首字母和尾字母转化成大写
String temp1 = "myproject";
String upString1 = temp1.substring(0,1).toUpperCase();
String upString2 = temp1.substring(temp1.length()-1,temp1.length()).toUpperCase();
String middle = temp1.substring(1,temp1.length()-1);
String result1 = upString1+middle+upString2;
System.out.println("result1 = "+result1);
//2 如何将字符串中的所有a替换成*
String temp2 = "mynameisapple";
String result2 = temp2.replace('a','*');
System.out.println("result2 = "+result2);
//3 如果字符串中包含子串src,如何取得第一个子串src后的所有字符
String temp3 = "D:/myproject/src/a/b/src";
String result3 = temp3.substring(temp3.indexOf("src/")+4,temp3.length());
System.out.println("result3 = "+result3);
}

}



result1 = MyprojecT
result2 = myn*meis*pple
result3 = a/b/src

3.Re:【求助】关于字符串处理的几个问题: [Re: lele] Copy to clipboard
Posted by: applelfish
Posted on: 2005-01-11 13:36

呵呵,谢谢lele,受益匪浅:)


   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