A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liyin 中级黑马   /  2014-6-3 17:37  /  1315 人查看  /  14 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class NewStr
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String str="super start";
  6.                 String newstr=str.replace("s","S");
  7.                 System.out.println(newstr);
  8.         }
  9. }
复制代码

上面字符串替换中,我只想替换super中的“s”,请高手指导

14 个回复

倒序浏览
  1. public class S{
  2.         public static void main(String[] args) {
  3.                 String str = "super start";
  4.                
  5.                 String s = str.replaceAll("s", "S");
  6.                 System.out.println(s);
  7.         }
  8. }
复制代码


只是把replace()变成replaceAll()就行;
回复 使用道具 举报 1 0
  1.                 String str = "super start";
  2.                 StringBuffer sb = new StringBuffer(str);
  3.                 sb.setCharAt(0, 'S');
  4.                 str = sb.toString();
  5.                 System.out.println(str);
  6.                
  7.                 //或者
  8.                 String str2 = "super start";
  9.                 String newStr2 = str2.replace("su", "Su");
  10.                 System.out.println(newStr2);
复制代码

评分

参与人数 1黑马币 +1 收起 理由
liyin + 1 很给力!

查看全部评分

回复 使用道具 举报 1 0
  1. public class S{
  2.         public static void main(String[] args) {
  3.                 String str = "super start";
  4.                 String s=str.replaceFirst("s", "S");
  5.                 System.out.println(s);
  6.                
  7.                
  8.         }
  9. }
  10. 看错问题了  不好意思
复制代码
回复 使用道具 举报 0 1
class NewStr
{
        public static void main(String[] args)
        {
                String str = "super start";

                String newstr = str.replaceAll("su", "Su");//第一种方法

                String str1 = str.replaceFirst("s", "S");//第二种方法

                System.out.println(newstr + "+++++++++++++" + str1);
        }
}
仔细看下API文档,你都可以做出来的
回复 使用道具 举报 1 0
学习了!
回复 使用道具 举报
把替换s 更改成替换su为Su   
回复 使用道具 举报
  顺便复习下字符串的替换
回复 使用道具 举报
More 发表于 2014-6-3 17:46
只是把replace()变成replaceAll()就行;

没有+all都是一样,只是想替换第一个,不是全部的。。。谢谢了
回复 使用道具 举报

哈哈,问题解决了,谢谢高手指导,原来只需要一点小变通{:3_53:}
回复 使用道具 举报

嘿嘿,相信你是没问题的
回复 使用道具 举报
提菩--空 发表于 2014-6-3 19:17
class NewStr
{
        public static void main(String[] args)

谢谢了,我是自己买的书自学的,还有现在也看毕老师的基础视频。。。突然遇到疑问就想到黑马论坛高手多就来问问,这不一下子就解决了{:3_53:}
回复 使用道具 举报

一起努力{:3_52:}
回复 使用道具 举报
凌羽猎风 发表于 2014-6-3 22:02
把替换s 更改成替换su为Su

谢谢了{:3_51:}
回复 使用道具 举报
茂子 发表于 2014-6-3 23:04
顺便复习下字符串的替换

温故而知新{:3_52:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马