package com.zyx.cn.baidu_test; /** 字符串"asd as da s" 將出現(xiàn)的第三個a置換成b;變?yōu)?asd as db s" * @author yltd * */ public class Test { public static void main(String[] args) { String s ="asd as da s"; int number =0; String str="" ; for (int i = 0; i < s.length(); i++) { String newChar = s.substring(i,i+1); if("a".equals(newChar)||newChar=="a"){ number++; if(number==3){ str= s.substring(0, i); str =str+"b"; str = str + s.substring(i+1, s.length()); System.out.println(str); } } } } }
|
|