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

熱點內容
如何把掃描文件做成pdf格式 瀏覽:624
php個性qq源碼 瀏覽:821
初學c語言顯示源未編譯 瀏覽:245
資產概況源碼 瀏覽:472
dos命令建文件夾命令 瀏覽:379
解壓的密碼htm被屏蔽 瀏覽:502
冬天太冷冰箱壓縮機不啟動怎麼辦 瀏覽:83
手機打開vcf需要什麼編譯器 瀏覽:910
加密磁碟後開機很慢 瀏覽:271
長沙智能雲控系統源碼 瀏覽:258
阿里雲伺服器如何設置操作系統 瀏覽:999
超級命令的英文 瀏覽:784
做賬為什麼要用加密狗 瀏覽:586
考研群體怎麼解壓 瀏覽:159
linux修改命令提示符 瀏覽:226
圓圈裡面k圖標是什麼app 瀏覽:63
pdf加空白頁 瀏覽:948
linux伺服器如何看網卡狀態 瀏覽:318
解壓新奇特視頻 瀏覽:707
圖書信息管理系統java 瀏覽:554