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

热点内容
刘毅10000词pdf 浏览:888
刚毕业的程序员会什么 浏览:966
单片机控制64路开关量 浏览:970
win10截图编程 浏览:418
怎样把名字变成文件夹 浏览:199
文件怎么搞成文件夹 浏览:726
多线程编程php 浏览:602
安卓机越用越卡有什么办法 浏览:7
高中生解压操场适合做的游戏 浏览:391
程序员java招聘 浏览:446
未来之光手机云服务器 浏览:158
服务器下载资料为什么c盘满了 浏览:263
怎么清除空文件夹 浏览:544
如何查看派派服务器 浏览:802
杀手6解压画面 浏览:669
夸张程序员 浏览:467
如何直播切两个APP画面 浏览:784
4x4测试服务器怎么获得 浏览:740
开环与闭环python 浏览:517
苹果手机上的东西怎么加密 浏览:554