⑴ java如何替换文本中所有的字符串ab,但abc中的ab不变
建议使用正则匹配
替换文本中所有的字符串ab,但abc中的ab不变
ab(?!c)
就是说如果现在要替换asdfgh,如果有asdfghjkl,这个地方不换,只有asdfgh前后不是英文字母才换
如果是独立单词的话:
asdfgh
如果是匹配前后不是英文字母的话:
[^a-zA-Z](asdfgh)[^a-zA-Z]?
⑵ 关于JAVA替换文本代码输出Usage: java ReplaceText File oldStr newStr
args.length != 3这个是定值,所以就输出哪个了。你应该是让变量对比,然后进行选择性的输出。
⑶ java中替换文本内容,使用传过来的字符串变量,只知道该字符串在文本中独占一行,请问如何查找替换他
用bufferedReader进行读取,按行读,String str=“”;
while ((str=bf.readLine())!=null){
if(str.equals(targetStr)){
//进行操作。
}
}
然后将str与传过来的字符串变量进行比较,看是否相同。
然后按你的规则进行查找替换。
⑷ Java文本编辑器 查找与替换功能如何实现
最简单的就是将文本内容作使用String处理
只替换一次 : String.replace("需要替换的字符串","替换的字符串")
替换所有匹配字符 : String.replaceAll("需要替换的字符串","替换的字符串")
replcaeAll支持使用正则表达式。
⑸ java正则如何替换掉这个字符串[]中的文本
String a = "aaaaa[asdasd asd asd]bbbbb";
a=a.replaceAll("\\[[^]]*]","");
System.out.println(a)
⑹ 用java编一个文本汉字替换程序
package com.;
import java.io.*;
public class ReplaceChinese {
public static void main(String[] args) {
String filePath = "F:\\workspace\\onlineChat\\src\\com\\\\ReplaceChinese.java";
File file = new File(filePath);
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
BufferedReader reader = null;
String result = "";// 用于存修改后的文字
String lineString = "";
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
for (int i = 0; i < tempString.length(); i++) {
if (tempString.substring(i, i + 1).matches(
"[\u4e00-\u9fa5]")) {
lineString += "[您要替换的代码]";
} else {
lineString += tempString.substring(i, i + 1);
}
}
result += lineString + "\n";
lineString = "";
line++;
}
reader.close();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("F:\\workspace\\onlineChat\\src\\com\\\\ReplaceChinese1.java")));
bw.write(result);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}
由于时间紧迫,只把功能实现了,优化你自己做做,能够成功运行
⑺ java项目中怎样批量替换一段文字
Ctrl+F,
⑻ java 替换文件内容
代码如下:
/***
* 方法:
* @Title: replaceContentToFile
* @Description: TODO
* @param @param path 文件
* @param @param str 开始删除的字符
* @param @param con 追加的文本
* @return void 返回类型
* @throws
*/
public static void replaceContentToFile(String path, String str ,String con){
try {
FileReader read = new FileReader(path);
BufferedReader br = new BufferedReader(read);
StringBuilder content = new StringBuilder();
while(br.ready() != false){
content.append(br.readLine());
content.append("\r\n");
}
System.out.println(content.toString());
int dex = content.indexOf(str);
if( dex != -1){
System.out.println(content.substring(dex, content.length()));
content.delete(dex, content.length());
}
content.append(con);
br.close();
read.close();
FileOutputStream fs = new FileOutputStream(path);
fs.write(content.toString().getBytes());
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}