導航:首頁 > 編程語言 > 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替換文本相關的資料

熱點內容
運動健康app華為手錶怎麼連接 瀏覽:748
肌肉塑造全書pdf下載 瀏覽:796
安卓簡約拼圖用什麼軟體好 瀏覽:289
fx1n加密程序 瀏覽:844
淘客阿里雲伺服器 瀏覽:476
100壓縮打造 瀏覽:422
安卓手機怎麼和蘋果平板傳文件 瀏覽:973
開始選項卡中的頁眉和頁腳命令選項 瀏覽:424
pdf的字體怎麼改 瀏覽:856
python讀寫視頻 瀏覽:88
科魯茲壓縮機軸承 瀏覽:353
word文檔轉換成pdf文件找不到 瀏覽:27
組件注冊命令 瀏覽:760
安卓大屏導航用的是什麼運放 瀏覽:443
myandroidtools的備份 瀏覽:900
python爬蟲天氣預報 瀏覽:761
android70許可權管理 瀏覽:749
魔獸辛迪加是什麼伺服器 瀏覽:472
電腦文件夾排序怎麼自定義排序 瀏覽:41
android70機型 瀏覽:422