导航:首页 > 编程语言 > java替换文本

java替换文本

发布时间:2022-08-12 06:43:21

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();

}

}

阅读全文

与java替换文本相关的资料

热点内容
学好单片机可以做什么东西 浏览:220
云服务器开放公网带宽安全么 浏览:17
java字节乱码 浏览:305
荣耀50什么时候升级安卓12 浏览:75
pdf文件怎么转成jpg 浏览:259
比泽尔压缩机代理商 浏览:629
什么app能下载多邻国 浏览:234
服务器蓝色什么意思 浏览:226
vb组态软件源码 浏览:698
加密蚊帐防蚊门帘 浏览:828
vs2010多线程编程 浏览:214
新股上市指标源码 浏览:699
c语言51单片机教程 浏览:554
app中的hd是什么意思 浏览:279
手机什么解压器最好 浏览:49
什么是适合自己的app 浏览:451
云服务器怎么补漏洞 浏览:176
如何看自己的安卓是什么等级 浏览:633
被丁磊挖走的程序员 浏览:19
gsk980ta编程 浏览:507