导航:首页 > 编程语言 > 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替换文本相关的资料

热点内容
做账为什么要用加密狗 浏览:583
考研群体怎么解压 浏览:156
linux修改命令提示符 浏览:224
圆圈里面k图标是什么app 浏览:59
pdf加空白页 浏览:945
linux服务器如何看网卡状态 浏览:316
解压新奇特视频 浏览:705
图书信息管理系统java 浏览:553
各种直线命令详解 浏览:862
程序员泪奔 浏览:147
素材怎么上传到服务器 浏览:516
android百度离线地图开发 浏览:189
web可视化编程软件 浏览:293
java笔试编程题 浏览:746
win11什么时候可以装安卓 浏览:564
java不写this 浏览:1001
云点播电影网php源码 浏览:97
pythonclass使用方法 浏览:226
移动加密软件去哪下载 浏览:294
php弹出alert 浏览:209