String str="1234563789 ";
String regEx="3";
boolean result=Pattern.compile(regEx).matcher(str).find();
System.out.println(result);
这个代码是查找字符串是否匹配 我现在想如果用这样的方法 把匹配的字符串修改成其他的字符串其他部分不变
意思就是把上面字符串中的"3" 替换成46 结果是"124645646789 " 或者替换成33 结果是"123345633789"
会的大大 请告诉我下代码 谢谢了 !
在线等
String regEx="3";
boolean result=Pattern.compile(regEx).matcher(str).find();
System.out.println(result);
这个代码是查找字符串是否匹配 我现在想如果用这样的方法 把匹配的字符串修改成其他的字符串其他部分不变
意思就是把上面字符串中的"3" 替换成46 结果是"124645646789 " 或者替换成33 结果是"123345633789"
会的大大 请告诉我下代码 谢谢了 !
在线等
arraycopy(from , fromIndex , to , toIndex , count);
Pattern.compile("3").matcher("123345633789").replaceAll("46")
http://topic.csdn.net/u/20081019/21/d84a671d-6762-4f68-9d3f-375de2756a4a.html
找到了。慢慢看。
以下代码质量不好,请楼下跳过。
public static void main(String[] args) {
String str = "1234563789 ";
String regEx = "3";
Matcher matcher = Pattern.compile(regEx).matcher(str);
boolean result = matcher.find();
System.out.println(result);
if (result) {
int idx = str.indexOf(matcher.group());
str=str.substring(0,idx)+"33"+str.substring(idx+regEx.length());
}
System.out.println(str);
}
public class TestString {
public static void main(String[] args){
String str="312345633789 ";
String regEx="3";
String ss="46"; Pattern p=Pattern.compile(regEx);
String[] s=p.split(str);
StringBuffer res=new StringBuffer();
for(int i=0;i<s.length;i++){
if(i==s.length-1){
res=res.append(s[i]);
}else{
res=res.append(s[i]+ss);
}
}
System.out.println(str);
System.out.println(res);
}
}
弱弱的问一下Pattern 为什么会语法错误呢?我敲了一遍不对 复制一次还是不对
要导这个包java.util.regex.Pattern;